Archived

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

adam17

Dynamic Arrays

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
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
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
i have tried setting the array to NULL but it doesnt change anything. im trying to implement the vector class into my program but i am getting confused on how to implement it with multiple, imbedded structures for example:

struct str1
{
int x, y, z;
};

struct str2
{
str1 *array1;
};

str2 *str2_array;

this is what i have right now and am having trouble converting it. does anyone have any ideas to solve this problem or my original problem?

Share this post


Link to post
Share on other sites
to make a vector of str2 type you have to do

struct str1
{
int x, y, z;
};
struct str2
{
vector <str1> str1_array;
};
vector <str2> str2_array;




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

[edited by - Thor82 on July 28, 2003 5:07:18 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:

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



This if statement is absolutley uneccessary, as the new operator will throw an exception on failure. The code should look something like this:

int *temp_array;
try
{
temp_array = new int[5];
}catch(...)
{
cout << "error";
// get out of program
}

Share this post


Link to post
Share on other sites
okay this is may come as a curveball but i solved one problem only to find another. the atoi() and atof() functions are not very accurate. if it reads in the string "1" it converts it to (int)1751347809. is there another function out there that i can use in place of these or am i missing an initialization of the functions.

Share this post


Link to post
Share on other sites
after doing a little more debugging i have started receiving this error:
First-chance exception in OGL Window.exe (NTDLL.DLL): 0xC0000005: Access Violation.
obviously its an access violation with one of my arrays (of type vector thanks Thor82), but what is bugging me is does it have anything to do with that NTDLL.DLL file? is it corrupted maybe?

Share this post


Link to post
Share on other sites
just to reinstate what im doing is reading in ASE files. Here is the import code:

while(!infile.eof())
{
infile >> stream;

if(stream == "*GEOMOBJECT")
{
num_obj++;
//Mesh_array = new MESH[1];


Mesh_array.resize(num_obj);

//outfile << "GEOMOBJECT " << num_obj << endl;

}


if(stream == "*MATERIAL_REF")
{
Mesh_array[num_obj].mat_id = atoi(stream.c_str());

//outfile << "MATERIAL_REF " << Mesh_array[num_obj].mat_id << endl;

}

if(stream == "*MESH_NUMTVERTEX")
{
infile >> stream;
Mesh_array[num_obj].num_t_vertex = atoi(stream.c_str());

//outfile << "NUM tVERTEX " << Mesh_array[num_obj].num_t_vertex << endl;

}

if(stream == "*MESH_NUMVERTEX")
{
infile >> stream;
Mesh_array[num_obj].num_vertex = atoi(stream.c_str());

Mesh_array[num_obj].vertex.resize(Mesh_array[num_obj].num_vertex);
Mesh_array[num_obj].vnormal.resize(Mesh_array[num_obj].num_vertex);

//outfile << "NUM mVERTEX " << Mesh_array[num_obj].vertex.size() << endl;

}

if(stream == "*MESH_NUMFACES")
{
infile >> stream;
Mesh_array[num_obj].num_faces = atoi(stream.c_str());

Mesh_array[num_obj].face.resize(Mesh_array[num_obj].num_faces);
Mesh_array[num_obj].fnormal.resize(Mesh_array[num_obj].num_faces);

//outfile << "NUM FACES " << Mesh_array[num_obj].face.size() << endl;

}

if(stream == "*MESH_VERTEX_LIST")
{
infile >> stream;
//outfile << "VERTEX LIST:" << endl;


for(int i=0; i<Mesh_array[num_obj].num_vertex; i++)
{
infile >> stream >> stream;

infile >> stream;
Mesh_array[num_obj].vertex[i].x = atof(stream.c_str());

//outfile << stream << " ";


infile >> stream;
Mesh_array[num_obj].vertex[i].z = atof(stream.c_str());

//outfile << stream << " ";


infile >> stream;
Mesh_array[num_obj].vertex[i].y = atof(stream.c_str());

//outfile << stream << " "<< endl;

}
}

if(stream == "*MESH_FACE_LIST")
{
infile >> stream;
//outfile << "FACE LIST:" << endl;


for(int i=0; i<Mesh_array[num_obj].num_faces; i++)
{
infile >> stream >> stream >> stream;

infile >> stream;
Mesh_array[num_obj].face[i].x = atoi(stream.c_str());

//outfile << stream << " ";


infile >> stream >> stream;
Mesh_array[num_obj].face[i].y = atoi(stream.c_str());

//outfile << stream << " ";


infile >> stream >> stream;
Mesh_array[num_obj].face[i].z = atoi(stream.c_str());

//outfile << stream << " " << endl;


for(int loop=0; loop<10; loop++)
infile >> stream;
}
}

if(stream == "*MESH_NORMALS")
{
infile >> stream;
//outfile << "MESH NORMALS" << endl;


for(int i=0; i<Mesh_array[num_obj].num_vertex; i++)
{
infile >> stream >> stream;

infile >> stream;
Mesh_array[num_obj].fnormal[i].x = atof(stream.c_str());

//outfile << stream << " ";


infile >> stream;
Mesh_array[num_obj].fnormal[i].z = atof(stream.c_str());

//outfile << stream << " ";


infile >> stream;
Mesh_array[num_obj].fnormal[i].y = atof(stream.c_str());

//outfile << stream << " ";


for(int loop=0; loop<3; loop++)
{
infile >> stream >> stream;

infile >> stream;
Mesh_array[num_obj].vnormal[i].x = atof(stream.c_str());

//outfile << stream << " ";


infile >> stream;
Mesh_array[num_obj].vnormal[i].z = atof(stream.c_str());

//outfile << stream << " ";


infile >> stream;
Mesh_array[num_obj].vnormal[i].y = atof(stream.c_str());

//outfile << stream << " ";

}
//outfile << endl;

}
}
}

...and here is where i read from the vectors and output to a text file for debugging.

GLuint Draw_Triangles()
{
double v1, v2, v3;
int index;

map = glGenLists(1);

glNewList(map, GL_COMPILE);
for(int loop1=0; loop1<num_obj+1; loop1++)
{
outfile << loop1 << endl;
glColor4f(1, 1, 1, 1);
for(int loop2=0; loop2<Mesh_array[loop1].num_faces; loop2++)
{
outfile << " " << loop2 << endl;
glBegin(GL_TRIANGLES); outfile << "Begin Triangles" << endl;

v1 = v2 = v3 = 0; outfile << "Init v1, v2, v3"<< endl;

index = Mesh_array[loop1].face[loop2].x; outfile << " " << index << endl;
v1 = Mesh_array[loop1].vertex[index].x; outfile << " " << v1 << endl;
v2 = Mesh_array[loop1].vertex[index].y; outfile << " " << v2 << endl;
v3 = Mesh_array[loop1].vertex[index].z; outfile << " " << v3 << endl;
glVertex3f(v1, v2, v3); outfile << " Vertex" << endl;

index = Mesh_array[loop1].face[loop2].y; outfile << " " << index << endl;
v1 = Mesh_array[loop1].vertex[index].x; outfile << " " << v1 << endl;
v2 = Mesh_array[loop1].vertex[index].y; outfile << " " << v2 << endl;
v3 = Mesh_array[loop1].vertex[index].z; outfile << " " << v3 << endl;
glVertex3f(v1, v2, v3); outfile << " Vertex" << endl;

index = Mesh_array[loop1].face[loop2].z; outfile << " " << index << endl;
v1 = Mesh_array[loop1].vertex[index].x; outfile << " " << v1 << endl;
v2 = Mesh_array[loop1].vertex[index].y; outfile << " " << v2 << endl;
v3 = Mesh_array[loop1].vertex[index].z; outfile << " " << v3 << endl;
glVertex3f(v1, v2, v3); outfile << " Vertex" << endl;
glEnd();
}
}
glEndList();

return map;
}

...and here is the output file:

0
0
Begin Triangles
Init v1, v2, v3
980710261

hope this helps

[edited by - adam17 on July 30, 2003 6:24:57 PM]

Share this post


Link to post
Share on other sites
To allocate memory, use malloc :
int* tmp_array = (int *)malloc( 5 * sizeof(int ) );
... a bit more complicated than new, but very accurate. I never allocated int with new, I thougth it was destinated for objects with constructors...

To convert string to int, use :
sscanf( src, "%d", &dest );

Hope it will help...

Share this post


Link to post
Share on other sites
to allocate memory for an array the function to be called should be calloc

so if the array is defined as
int* MyArray=NULL;
i usually do

MyArray= (int*) calloc( how_many_items,sizeof(item)); (anyway it should work the same...)

to read from file i usually use fscanf, it is in C though, not c++,
i don't remember how the >> operator works, does it stop reading when it find a space? or a \n? or what?

by the way...
if in the file you have
string int float \n
with fscanf i use:
fscanf(FileIn,"%s %d %f\n",Mystring,&MyInt,&MyFloat);

and this worked for a 3DMax Parser...
try some of this



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

[edited by - Thor82 on July 31, 2003 6:17:19 AM]

Share this post


Link to post
Share on other sites
my arrays are finally FIXED!!!!! thank you guys so much. there is one problem that i hope you can help me out with. as you already know im reading my data from an ASE file. im reading the data into strings and then using atof() for vertices and atoi() for index values. atoi() works okay its atof() that is screwing up. for example i read in the values
-6.9363, pass it through atof(), then it returns as 3.0637.
-1.9363, pass it through atof(), then it returns as 8.0637.
here is where it is really weird:
-2.5000, pass it through atof(), then it returns as -2.5!!
any ideas!?!?
once again thank you for all of the help!

Share this post


Link to post
Share on other sites
goto to VECTOR !!!!
use push_back to store data.
use like array when data is stored
use .empty or clear to wipe it
and it's easy!

pushback classes and structs

and it has only physical ram limits.


Now, as for the raw data to atoi and atof.
post a code snibbit.




[edited by - BGCJR on August 1, 2003 11:54:08 PM]

Share this post


Link to post
Share on other sites
here you go:

if(stream == "*MATERIAL_REF")
{
Mesh_array[num_obj].mat_id = atoi(stream.c_str());

//outfile << "MATERIAL_REF " << Mesh_array[num_obj].mat_id << endl;

}


if(stream == "*MESH_VERTEX_LIST")
{
infile >> stream;
//outfile << "VERTEX LIST:" << endl;


for(int i=0; i<Mesh_array[num_obj].num_vertex; i++)
{
infile >> stream >> stream;

infile >> stream;
Mesh_array[num_obj].vertex[i].x = atof(stream.c_str());

//outfile << stream << " ";


infile >> stream;
Mesh_array[num_obj].vertex[i].z = atof(stream.c_str());

//outfile << stream << " ";


infile >> stream;
Mesh_array[num_obj].vertex[i].y = atof(stream.c_str());

//outfile << stream << " "<< endl;

}
}

Share this post


Link to post
Share on other sites
try:

if in the file you have
string int float \n
(example:
*dontrememberwhat 5 -83.45\n)

with fscanf i use:
fscanf(FileIn,"%s %d %f\n",Mystring,&MyInt,&MyFloat);

and i''ll have
Mystring = "*dontrememberwhat"
MyInt = 5
Myfloat = -83.45

and file offset positioned at the first char of the next line




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

Share this post


Link to post
Share on other sites
Thor82 im not clear on what your saying. does what you said have anything to do with converting string values to integers or float values? im not sure.

Share this post


Link to post
Share on other sites