We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,527 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Arrays and Pointer Arithmetic Question

Hello, I'm not sure that this shouldn't be in the Computer Science Board because it is mostly a theory question, but 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.

1
Contributor
0
Replies
1
View
sciwizeh
Posting Pro in Training
487 posts since Jun 2008
Reputation Points: 77
Solved Threads: 25
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0556 seconds using 2.66MB