Hello, I'm not sure that this shouldn't be in the C but it is mostly a theory question, although it is also implemented in C, so here it goes.

I've been looking at some ways to make a modifiable array to hold geometric data for a simplistic 3D program (using OpenGL). Looking through the web, and I found one way to store changeable information something along the lines of:

typedef GLfloat point[3];

int number=5;
point *polygon;
...
void addPoint(point p){
   number ++;
   *(point+number-1)=p;
}

Now that may not be right, I haven't actually tried to implement anything yet, because for some reason this doesn't seem ... safe.

It seems as though this could be bad if other items are in the heap. For example, for simplicities sake lets say that the heap can hold 10 integers. So, a pointer to the first position is like the polygon pointer above, and another is made that is in the fifth position. If number gets to or exceeds 6 then whatever integer was last added replaces what happens to be in the fifth position.

Will this happen? And is there a way to prevent it? And how else can this be accomplished, possibly in a safer way?

A link will suffice, just something on the theory behind this. Something that will help me understand this concept and how it is or isn't hazardous to do.

If I'm not being clear enough please ask for clarification.

Thanks.

This is a C question and belongs in the C forum.

It was in the C forum, but nobody said anything.
http://www.daniweb.com/forums/thread170785.html

But then again, I didn't understand what all the 5th and 6th stuff was all about.

> So, a pointer to the first position is like the polygon pointer above, and another is made that is in the fifth position
5th position of what, polygon or the heap?

If you have
point *polygon = malloc ( 10 * sizeof *polygon );
then you've got polygon[0] through to polygon[9]

Any more, and you're off in the weeds somewhere.

so, is there a way to re-molloc it? like do use a temporary array then delete that pointer and realocate space and fill it with the temporary array and new value(s)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.