Skip to main content
GameDev.net gamedev.net
🔒 Locked

Array Problems

Started by neonoblivion Apr 7, 2005 at 4:33 AM 22 replies 3.6k views
jollyjeffers
jollyjeffers
what type is it? that number would only be ~100-200kb for most basic primitives, but if you're allocating large structures then you could be running out of heap space.

Also, what language are you using? it could be that it's limiting you to the top end of a signed 16bit index / uses 16bit array indicies.

We need more details to answer this question accurately [smile]

hth
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
CloudNine
CloudNine
Posting some code (maybe the data type you're allocating) and how you allocate it would help a lot.
CloudNine
CloudNine
Unless vers is a unimaginably high number, allocating that is not your problem. Are you sure it's crashing out on that line?
neonoblivion
neonoblivion
for(unsigned long int index2=0;index2<models[levelmodels[index].modelindex].nump*3;index2+=3){        //test if the poly is in the function         float x1=models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)index2*3]*3],               z1=models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)index2*3]*3+2],               x2=models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+1)*3]*3],               z2=models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+1)*3]*3+2],               x3=models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+2)*3]*3],               z3=models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+2)*3]*3+2];         float y1=models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)index2*3]*3+1]+getheight(x1,z1),               y2=models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+1)*3]*3+1]+getheight(x2,z2),               y3=models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+2)*3]*3+1]+getheight(x3,z3);         if(triangleinfrustum(x1,y1,z1,x2,y2,z2,x3,y3,z3)){            glBegin(GL_TRIANGLES);               getlightcolor(&c,models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)index2*3]*3],models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)index2*3]*3+2]);               glColor3f(c.r,c.g,c.b);               glTexCoord2f(models[levelmodels[index].modelindex].texcords[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)index2*3+1]*2],models[levelmodels[index].modelindex].texcords[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)index2*3+1]*2+1]);   			   glVertex3f(x1,y1,z1);               getlightcolor(&c,models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+1)*3]*3],models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+1)*3]*3+2]);               glColor3f(c.r,c.g,c.b);   			   glTexCoord2f(models[levelmodels[index].modelindex].texcords[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+1)*3+1]*2],models[levelmodels[index].modelindex].texcords[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+1)*3+1]*2+1]);   			   glVertex3f(x2,y2,z2);               getlightcolor(&c,models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+2)*3]*3],models[levelmodels[index].modelindex].vers[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+2)*3]*3+2]);               glColor3f(c.r,c.g,c.b);   	  	    	glTexCoord2f(models[levelmodels[index].modelindex].texcords[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+2)*3+1]*2],models[levelmodels[index].modelindex].texcords[(unsigned int)models[levelmodels[index].modelindex].polys[(unsigned int)(index2+2)*3+1]*2+1]);   			   glVertex3f(x3,y3,z3);            glEnd();         }     	}


...actually upon furthure examination it dies here, after iterating a bunch...
CloudNine
CloudNine
You could be indexing beyond an array somewhere. Perhaps simplifying your code a bit would help, for example,

models[levelmodels[index].modelindex] //as i've seen multiple times can become..Model* currModel=models[levelmodels[index].modelinux;


I have to admit, that code you posted up there is not that easy to follow.
jollyjeffers
jollyjeffers
Quote:
Original post by neonoblivion
*** Source Snippet Removed ***

...actually upon furthure examination it dies here, after iterating a bunch...

I'll put my money on CloudNine's idea about array indexing. You've got a helluva lot of array look-ups in there, debugging the actual elements it's reading/writing will probably tell you what index is wrong, and then you can work out why [smile]

hth
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
Couvillion
Couvillion
Quote:
Original post by Anonymous Poster
Quote:
Original post by neonoblivion
float *pver=NULL,*ptex=NULL,*pnor=NULL;

pver=(float *)malloc(sizeof(float)*vers*3);


May I ask why --- since this is C++ -- you aren't using the following?:
pver = new float[vers*3];


Or even

vector<float> pver(vers*3);  //May want to use val_array<float>


Then your code can crash the C++ way.
~~C--O -
Ted_Striker
Ted_Striker
yeah I'd also say simplify your array indexing a bit. thats probably your problem.

About your culling, use a bounding volume such as a sphere or box instead of checking every triangle. Thats definaltly gonna be a death-nail when you get this working.
neonoblivion
neonoblivion
I don't use "new" because malloc works, and I fear change :)
here's my new code, revised as Cloud suggested:
for(unsigned int index2=0;index2<tempmodel->nump*3;index2+=3){         sprintf(errorstr,"%i",(unsigned long)index2*3);      error(errorstr);        //test if the poly is in the function         float x1=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)index2*3]*3],               z1=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)index2*3]*3+2],               x2=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+1)*3]*3],               z2=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+1)*3]*3+2],               x3=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+2)*3]*3],               z3=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+2)*3]*3+2];         float y1=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)index2*3]*3+1]+getheight(x1,z1),               y2=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+1)*3]*3+1]+getheight(x2,z2),               y3=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+2)*3]*3+1]+getheight(x3,z3);         if(triangleinfrustum(x1,y1,z1,x2,y2,z2,x3,y3,z3)){            glBegin(GL_TRIANGLES);               getlightcolor(&c,tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)index2*3]*3],tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)index2*3]*3+2]);               glColor3f(c.r,c.g,c.b);               glTexCoord2f(tempmodel->texcords[(unsigned int)tempmodel->polys[(unsigned int)index2*3+1]*2],tempmodel->texcords[(unsigned int)tempmodel->polys[(unsigned int)index2*3+1]*2+1]);   			   glVertex3f(x1,y1,z1);               getlightcolor(&c,tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+1)*3]*3],tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+1)*3]*3+2]);               glColor3f(c.r,c.g,c.b);   			   glTexCoord2f(tempmodel->texcords[(unsigned int)tempmodel->polys[(unsigned int)(index2+1)*3+1]*2],tempmodel->texcords[(unsigned int)tempmodel->polys[(unsigned int)(index2+1)*3+1]*2+1]);   			   glVertex3f(x2,y2,z2);               getlightcolor(&c,tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+2)*3]*3],tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)(index2+2)*3]*3+2]);               glColor3f(c.r,c.g,c.b);   	  	    	glTexCoord2f(tempmodel->texcords[(unsigned int)tempmodel->polys[(unsigned int)(index2+2)*3+1]*2],tempmodel->texcords[(unsigned int)tempmodel->polys[(unsigned int)(index2+2)*3+1]*2+1]);   			   glVertex3f(x3,y3,z3);            glEnd();         }     	}


The weird thing is why would it only crash with a large number of verts and not a smaller if it's an array bounds issue?
Palidine
Palidine
Quote:
Original post by neonoblivion
I don't use "new" because malloc works, and I fear change :)


To pre-empt Oluyesi: "if you don't want to use C++ then don't use C++. new is for C++ so you should be using it or sticking with C"

And related to your crash, have you run it in a debugger to see where it's crashing and why? I'll agree with others that your code is almost impossible to follow.

-me
Palidine
Palidine
Disregarding the new/malloc issuse, have you run this in a debugger to see what the problem is?

-me
ProPuke
ProPuke
Quote:
new is for C++ so you should be using it or sticking with C

do not misinterpret - new & malloc are not mutually exclusive.
new is an operator used to initialise structure while malloc is a function used to allocate it. However, new often allocates the memory aswell (like in the default use), but it needn't.
I use malloc & use an overloaded new operator to initialise the space manually afterwards. That way I can be sure that I'm using the malloc set & am perfectly safe to use realloc (aslong as I manually call the deconstructors for any that are being cropped or use the new operator to initialise any extra space)
____ _ ___ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
Zahlman
Zahlman
x1=tempmodel->vers[(unsigned int)tempmodel->polys[(unsigned int)index2*3]*3],


Good god.

Strongly consider making some use of OO. Or at least write a free function to access vertices from a model pointer. I shudder to think what your structure looks like, btw...
Palidine
Palidine
(removed the (unsigned int) casting to make it a tad more readable:

tempmodel->vers[ tempmodel->polys[ index2*3 ]*3 ]

I'm not sure what you are doing here, but it seems excessive. Why do you have polys,verts, normals and texcoords in a struct? What's a "poly"? Why are you indexing polys by index*3? and why is that multiplied *3 to get the vertex? It seems like an incrediably confusing model structure. For reference this is basically what I use (forgive syntactical typos...I'm reconstructing from memory):

struct Model{    unsigned int numIndices;    GLuint *indices;    GLfloat *vertices;    GLfloat *normals;    GLfloat *texcoords;    GLuint texture;}


to draw you then just do the following without any need for confusing array indexing:
glEnableClientState(GL_INDEX_ARRAY);glEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_NORMAL_ARRAY);glEnableClientState(GL_TEXTURE_COORD_ARRAY);glIndexPointer(GL_INT,0,normals);glTexCoordPointer(2,GL_FLOAT,0, texcoords);glNormalPointer( GL_FLOAT, 0, normals );glVertexPointer( 3, GL_FLOAT, 0, vertices );glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D,  decal);glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0 );


If you want to use some kind of keyframe animation (which is the only thing that i can think that "polys" represents) you can do a slight redesign of the Model struct so that each "frame" just gets a seperate vertices list.

-me

P.S.

Quote:
Original post by ProPuke
do not misinterpret - new & malloc are not mutually exclusive.
new is an operator used to initialise structure while malloc is a function used to allocate it.


Yes. I understand that malloc is still useful in a C++ environment. The reason that I typically say "new is for C++" is that we occasionally get threads here with C-coders complaining that their constructors never seem to get called. The danger with "refusing to use new" is that you miss one of the most powerful features of C++: namely object initialization on creation. It makes sense to use malloc if you know why new is there and you are explicitly choosing to forego the object initialization step.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.