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 ++;

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.


9 Years
Discussion Span
Last Post by Salem

It was in the C forum, but nobody said anything.

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)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.