Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8810719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:11:13+00:00 2026-06-14T03:11:13+00:00

I have a problem trying to load a 3d object from a wavefront file

  • 0

I have a problem trying to load a 3d object from a wavefront file and saving it into a structure that I predefined. I get a segmentation fault error, I`m assuming that I have gone wrong with my pointers somewhere. Here are the sructures:

struct vertex
{
    float x,y,z;
};

// the idea is to split each object into groups in order to animate
// them seperately
struct group
{
    float xpos, ypos, zpos;
    float xrot, yrot, zrot;

    int numVertices;
    int numFaces;

    //all verteces
    vertex vertices[MAX_NUM];

    //faces-- a face is made up of one vertex from each of these arrays
    vertex vertexOne[MAX_NUM];
    vertex vertexTwo[MAX_NUM];
    vertex vertexThree[MAX_NUM];
};

struct object
{
    float xpos, ypos, zpos;
    float xrot, yrot, zrot;

    int numGroups;
    group * groups[MAX_NUM];
};

This is the function that should load the object:

object * loadObject(char * path)
{
object * obj;
obj = new object;

char str[1];
obj->numGroups = 0;
FILE * file = fopen(path, "r");

while(fscanf(file, "%s", str) != EOF)
{
    if(str[0] == 'o')
    {
        obj->numGroups++;
        obj->groups[obj->numGroups-1] = new group;

        obj->groups[obj->numGroups-1]->numVertices = 0;
        obj->groups[obj->numGroups-1]->numFaces = 0;
    }
    else if(str[0] == 'v' && str[1] == ' ')
    {
        obj->groups[obj->numGroups-1]->numVertices++;
        sscanf(str, "v %f %f %f",
        &(obj->groups[obj->numGroups-1]->vertices[obj->groups[obj->numGroups-1]->numVertices-1].x),
        &(obj->groups[obj->numGroups-1]->vertices[obj->groups[obj->numGroups-1]->numVertices-1].y),
        &(obj->groups[obj->numGroups-1]->vertices[obj->groups[obj->numGroups-1]->numVertices-1].z));
    }
    else if(str[0] == 'f')
    {
        int one, two, three;
        obj->groups[obj->numGroups-1]->numFaces++;
        sscanf(str, "f %i/%*i/%*i %i/%*i/%*i %i/%*i/%*i", &one, &two, &three);

        obj->groups[obj->numGroups-1]->vertexOne[obj->groups[obj->numGroups-1]->numFaces-1] = obj->groups[obj->numGroups-1]->vertices[one-1];
        obj->groups[obj->numGroups-1]->vertexTwo[obj->groups[obj->numGroups-1]->numFaces-1] = obj->groups[obj->numGroups-1]->vertices[two-1];
        obj->groups[obj->numGroups-1]->vertexThree[obj->groups[obj->numGroups-1]->numFaces-1] = obj->groups[obj->numGroups-1]->vertices[three-1];
    }
}

fclose(file);
return obj;
}

I have tried for quite a while to find the problem but i get no information on the error at all. I know the way I`m assigning the vertices and faces is a bit tedious, any suggesstions for neater code are very welcome. Please tell me if any additional information is needed, thank you very much in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:11:14+00:00Added an answer on June 14, 2026 at 3:11 am

    First you do

    obj->numGroups = 0;
    

    Then in some cases you do

    obj->groups[obj->numGroups-1]
    

    without increasing obj->numGroups, meaning you access the array way out of bounds. You also don’t check if the array entry been allocated or not.


    Another problem is that str is only one character, and your fscanf scans for a string which will put at least two character into the string: The actual character you want and then the string terminator character. You also access str[1] later in the loop, which is out of bounds. All of this is undefined behaviour and can lead to crashes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problems trying to load png file into my application. It seems to
I have a problem with out of memory when I'm trying load a few
Ok I have this problem I'm trying to use Jquery to load a partial
I have a problem trying to turn my python code into an executable using
I have a problem trying to hide .php extension from the url I have
I have been struggling with a problem trying to get PLJava to work on
well the problem is that i am trying to get DDL to: 1. Recive
I have created a website from File->new-> Web Site. i am trying to go
I'm trying to load a class from a JAR file using reflection. This JAR
I have a problem trying to register my own Event/Listener to the event dispatcher.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.