🔒 Locked
Array Problems
Advertisement
Original Post
Is there a reason an array of more than 32,768 segments would crass my program?
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
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
I am dynamically allocating the array, and it in an array of floats... stack overflow sounds scary TToTT
oh yeah and I'm using c++ with Dev-c++.
float *pver=NULL,*ptex=NULL,*pnor=NULL;
pver=(float *)malloc(sizeof(float)*vers*3);
pver=(float *)malloc(sizeof(float)*vers*3);
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...
You could be indexing beyond an array somewhere. Perhaps simplifying your code a bit would help, for example,
I have to admit, that code you posted up there is not that easy to follow.
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.
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
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.
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.
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.
I don't use "new" because malloc works, and I fear change :)
here's my new code, revised as Cloud suggested:
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?
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?
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
I far prefer c style structured code, but I also like to take advantage of elements of polymorphism and the abbility to define vars anywhere in my code.
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)
typedef struct model{ bool loaded; unsigned long nump,numt,numv,numn; unsigned long * polys; float * vers; float * normals; float * texcords; float x,y,z,xr,yr,zr,xs,ys,zs;} model;
(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):
to draw you then just do the following without any need for confusing array indexing:
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.
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.
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.
Advertisement