Corrupted Stack From an Array of Structs

Started by
7 comments, last by Zahlman 15 years ago
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: stack corrupted error Thankyou.
Advertisement
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 = cv2;<br><br>i += 2;<br></pre><br>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.<br><br>Also, consider using for loops to compress the loop management into a single line.<br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br><span class="cpp-keyword">for</span>( <span class="cpp-keyword">int</span> z = <span class="cpp-number">0</span> ; z &lt; TER_ZLENGTH ; ++z )<br>{<br>	<span class="cpp-keyword">for</span>( <span class="cpp-keyword">int</span> x = <span class="cpp-number">0</span> ; x &lt; TER_XLENGTH ; ++x)<br>	{<br>		<span class="cpp-comment">// …</span><br>	}<br>}<br><br></pre></div><!–ENDSCRIPT–> 
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++;	}
Quote:Original post by alexgeek
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:

*** 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.
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.
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.
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?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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.
Quote:Original post by alexgeek
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.


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.

This topic is closed to new replies.

Advertisement