# Corrupted Stack From an Array of Structs

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

## Recommended Posts

I've spent the last few hours working out how to dynamically create set of tiled vertices for a floor. All that remains now is putting all of the information from the while loop into an array of the struct CUSTOMVERTEX.
struct CUSTOMVERTEX verts[TER_XLENGTH*TER_ZLENGTH]; // 8 * 12
int i = 0;
int z = 0;
int x = 0;
while(z < TER_ZLENGTH)
{
x = 0;
while(x < TER_XLENGTH)
{
int cur_x = x;
CUSTOMVERTEX cv,cv2;
cv.X = cv2.X = x;
cv.Y = cv2.Y = 0;
cv.Z = z;
cv2.Z = z+1;
cv.COLOR = cv2.COLOR = 0xffffff;
cv.U = cv2.U = x;
cv.V = cv2.V = z;
verts = cv;
i++;
verts = cv2;
x++;
i++;
}
z++;
}


It seems to be
struct CUSTOMVERTEX verts[TER_XLENGTH*TER_ZLENGTH];
that is the main problem, I've tried a few different ways, but to be quite honest I don't know how to have an array of structs, google was not my friend either. The error I get is: Thankyou.

##### Share on other sites
The array "verts"has TER_XLENGTH * TER_ZLENGTH elements.

Your nested loops iterate over these two ranges. Inside the inner loop, you effectively do this:
verts = cv;verts[i + 1] = cv2;i += 2;

So basically, you are walking out of the bounds of your array in a serious fashion. You array should be twice the size it is now for this to work.

Also, consider using for loops to compress the loop management into a single line.
for( int z = 0 ; z < TER_ZLENGTH ; ++z ){	for( int x = 0 ; x < TER_XLENGTH ; ++x)	{		// ...	}}

##### Share on other sites
That worked perfectly thankyou.
But there seems to be some problem with my miracle tile making algorithim.

The last (x) row of tiles seems to not appear, checking in wireframe mode they definetely aren't there but there are two sometimes invisible lines jetting off far into the distance.

Can anyone see what the problem is? My code now is as follows:

	struct CUSTOMVERTEX verts[2*TER_XLENGTH*TER_ZLENGTH];	int i = 0;	int z = 0;	int x = 0;	while(z < TER_ZLENGTH)	{		x = 0;		while(x < TER_XLENGTH)		{			if(x == 7)				textmgr.Console_Add("got to 7");			if(x == 8)				textmgr.Console_Add("got to 8");			int cur_x = x;			CUSTOMVERTEX cv,cv2;			cv.X = cv2.X = x*SCALE_MULT; cv.Y = cv2.Y = 0; cv.Z = z*SCALE_MULT; cv2.Z = (z+1)*SCALE_MULT;			cv.COLOR = cv2.COLOR = 0xffffff;			cv.U = cv2.U = x*SCALE_MULT; cv.V = cv2.V = z;			verts = cv;			i++;			verts = cv2;			x++;			i++;		}		z++;	}

##### Share on other sites
Quote:
 Original post by alexgeekThat worked perfectly thankyou.But there seems to be some problem with my miracle tile making algorithim.The last (x) row of tiles seems to not appear, checking in wireframe mode they definetely aren't there but there are two sometimes invisible lines jetting off far into the distance.Can anyone see what the problem is? My code now is as follows:*** Source Snippet Removed ***

		while(x < TER_XLENGTH)		{			if(x == 7)				textmgr.Console_Add("got to 7");			if(x == 8)				textmgr.Console_Add("got to 8");

Wasn't TEL_XLENGTH 8? If that is so then on the moment x reaches 8 it goes out of for loop and never reaches if(x==8) clause. Is that the issue? Never reaching 8? Array indexes goes 0...size-1 so if size is 8 then biggest array index is 7.

##### Share on other sites
Stop. You really need to understand this part first, because otherwise you're getting wayyyyy ahead of yourself.

An array of size N has N many valid indices. They range from 0 to N-1, inclusive.

If you iterate with 'i' over an array of 8 ints, their indices are 0, 1, 2, 3, 4, 5, 6 and 7.

##### Share on other sites
Yes but I still don't see why the last column isn't being drawn.
Instead I get this strange plane shooting off somewhere:

I don't understand how that could happen.
Thanks guys.

##### Share on other sites
Think about how many verticies you need to draw for an n*m grid. First off, how many for a 1x1 grid = 4 verticies.
How many for a 1x3 grid = 8 verticies.
You need (n+1)*(m+1) verticies for an n*m grid! (n*m) is not enough!

What is cur_x for? Is it unnecessary, or is it a sign that there is other code in that function which uses it that you have left out of what you've posted here?

##### Share on other sites
I see your logic in an m+1*n+1 sized array but that causes the stack to corrupt.
My array is sized 2*m*n.
But none of that explains why the last column isn't drawn and the random massive plane shooting off into the distance :/
thanks.

##### Share on other sites
Quote:
 Original post by alexgeekI see your logic in an m+1*n+1 sized array but that causes the stack to corrupt.My array is sized 2*m*n.

Look.

You write two CUSTOMVERTEX instances per vertex, right?

1) Why is that?

2) No matter how many CUSTOMVERTEX instances you write per vertex, you still need (m+1)*(n+1) vertices to describe an m*n grid of squares.

Here. Let me give you a slightly tedious assignment.

Let's say we want to make a 2x2 grid.

Tell me exactly how many CUSTOMVERTEX instances you think are needed, and exactly what the value is of each member of each instance. And to prove it, show the code where you use the CUSTOMVERTEX array to draw stuff.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633329
• Total Posts
3011382
• ### Who's Online (See full list)

There are no registered users currently online

×