Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

adam17

Dynamic Arrays

This topic is 5526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok i hope this is my last message requesting help on arrays, but not my last message here. anyway, im trying to make my own model loader so i can have the experience. well im reading in the vertices into dynamic arrays, not linked lists. here is how i declared the arrays: int *tmp_array; ... tmp_array = new int[5]; now allocating the arrays is the easy part. the problem im having is reading from them via the [ ] operator. it gives me an access violation whenever i try. any ideas what i could do to read from them?

Share this post


Link to post
Share on other sites
Advertisement
Hi!

My guess is that youre trying to access elements in the array that don''t exist. For example,

int* tmp_array = new int[5];

tmp_array[5] = something; // this will crash!!

if you declare an array with a size of 5, it will have 5 elements indexed from 0 to 4. You have to know how many elements you will need, and never use any more than what you allocated.

Hope this helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Juggers
int* tmp_array = new int[5];

tmp_array[5] = something; // this will crash!!




that doesn''t necessarily crash... if theres something allocated after it''ll write to it... sometimes

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
quote:
Original post by Juggers
int* tmp_array = new int[5];

tmp_array[5] = something; // this will crash!!




that doesn''t necessarily crash... if theres something allocated after it''ll write to it... sometimes




true, but that''s even worse, since it will make a completely different part of the program buggy and you won''t know what''s going on.

When I work with arrays I almost always put the line:

assert(index < array_size);

right before the array access, and I highly recommend it.

Share this post


Link to post
Share on other sites
You should always check to see if the allocated space is not null
int *temp_array;
temp_array = new int[5];
if (temp_array==NULL)
{
cout<<"error"< //get out of program
}
//go on


Share this post


Link to post
Share on other sites
that was fast!
nm. i forgot to mention that i have a global variable (naughty me) that stores the length of the array. basically i read in the number of vertices and its stored in a variable. then i read in the vertices with the keeping in mind the length variable. i dont get the error when im writing to the array, just when im reading from it. im traversing the array from 0 ~ num_vertices-1. my prog will not even read one element. hope that helps.

Share this post


Link to post
Share on other sites
ok forget my last post. i just did a little debugging of my code and discovered that nothing is being written to the arrays! as a matter of fact the arrays == NULL. am i allocating the arrays wrong (tmp_array = int new[5]) because when i store a value into it i dont get an error. any ideas?

Share this post


Link to post
Share on other sites
quote:
Original post by adam17
ok forget my last post. i just did a little debugging of my code and discovered that nothing is being written to the arrays! as a matter of fact the arrays == NULL. am i allocating the arrays wrong (tmp_array = int new[5]) because when i store a value into it i dont get an error. any ideas?


I don''t know if you can allocate arrays like that. I use:
tmp_array = new int[5];

Share this post


Link to post
Share on other sites
try using the stl "vector"...it''s easier and it handles everything

or try using old c calloc ( array= (int*) calloc(n,sizeof(int)))

in this way it stores n*sizeof(int) bytes...
it''s not the best way though....



There aren''''t problems that can''''t be solved with a gun...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!