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?
Dynamic Arrays
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:
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.
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.
July 27, 2003 01:09 AM
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
quote:
Original post by Anonymous Posterquote:
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.
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
int *temp_array;
temp_array = new int[5];
if (temp_array==NULL)
{
cout<<"error"<
}
//go on
The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.
?Have a nice day!?
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.
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.
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?
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];
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...
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...
There aren''t problems that can''t be solved with a gun...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement