0

I have a pointer array of a custom Vector typedef (just 3 floats). I'm writing a Wavefront object loader and I want to be able to add to an array of these vectors whenever a vertex row in the file is encountered. My problem at the moment is allocating memory for the new vector.

Vector

typedef struct
{
    float x;
    float y;
    float z;
}
Vector;

Parsing the line and saving in memory

Vector *verticies;

Vector tmp;
verticies = new Vector;

long endPtr = sizeof(verticies);

sscanf(line, "v %f %f %f", &verticies[endPtr].x, &verticies[endPtr].y, &verticies[endPtr].z);

return;

There are eight vertices in the .obj file and the output of sizeof(verticies) shows 8 all the time. The problem with the code above is that the new vertex is given to the last element in *verticies, so my question is; ** how do I add elements to the end of a pointer array dynamically?**

3
  • Hi, are you sure the C++ tag is appropriate here? The only C++ thing I can see is operator new. Everything else is pure C. Commented Jul 30, 2011 at 14:03
  • Then yes, it is. This is also wrapped in a class. Commented Jul 30, 2011 at 14:04
  • Wrapping something in a class doesn't make it C++, it makes it C with classes. ;) Commented Jul 30, 2011 at 14:16

4 Answers 4

1

You are allocating space for exactly one Vector struct.

sizeof(vertices) will be the size of a pointer on your machine and is absolutely meaningless here.

sscanf(line, "v %f %f %f", &(verticies->x), &(verticies->y), &(verticies->z));

is going to do what you need. But this only enabling you to read in a single Vector. You either need to allocate enough space (as many vectors as you have lines) and use a for loop to match the line with the offset in the array.

But you really should be using std::vector and a std::ofstream.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to mark this as the correct answer, but I've decided to move to std::vector in lue of yours and others advice. Thanks for your help.
1
sizeof(verticies);

always gives 8, because it is a pointer, size of pointer on your environment is 8.

It does not mean you have 8 vertices.

If you want array of Vector of 8 items, you need to do:

Vector verticies[8];

If you do not know how many Vector items you need to use at compile time you should use,
std::vctor.

Comments

1

You should be using a std::vector for this. (Or some other standard container.)

Nothing in your code contains an array of pointers. All you have is a pointer to a single Vector item that you created with new Vector.

sizeof(vertices) will give you the size of vertices which is a Vector*. So sizeof(vertices) will always return whatever the size of a pointer is on your platform.

3 Comments

If OP already knows the size of number of elements at compile time, s/he need not use std::vector at all, a simple array would suffice.
"I want to be able to add to an array of these vectors whenever a vertex row in the file is encountered" - I don't think s/he does
He doesn't :-) I wrote a loader before that iterated through the file once to count the number of verticies, faces, etc, allocated the correct amount of memory and then went through it again loading the data into RAM. Less than ideal, which is why I asked my question.
1
Vector *verticies;
verticies = new Vector;

First of all, verticies is not an array of pointers of type Vector. It's just a pointer to Vector. So, when ever you creating a instance, vecticies is pointing to it. Leaving the last instance it was pointing to behind causing memory leak. If you need array of pointers, then it would be -

Vector *verticies[constant];

Since you tagged C++, std::vector is apt for your program.

std::vector <Vector> verticies ;

Now, do a push_back operation for each instance you create.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.