• Advertisement

Archived

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

Problems with pointers (or stack... depending on how you look at it.)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, so I've got a decent bit of programming experience, but most of it's in VB, not C++. Thus, I'm still getting to know my way around in C++. In a mapmaker program, there are 199x199 tiles. Each tile has 4 vertices, each of which has a float value assigned to it for shading. Tiles[0->199][0->199].Vertex[0->3].Shading Now, I essentially need to "copy" the shading information to a buffer, so that I can blend the copied buffer without affecting the real thing until the entire process is complete. If I change the real thing as I go, then tiles begin to blend with previous blended tiles. I want a uniform blending. Thus, here's the code I was using:
void BlendLighting()
{
	float SS[200][200][4];
	for(int x = 0; x < 200; x++)
	{
		for(int z = 0; z < 200; z++)
		{
			//TOP LEFT//

			SS[x][z][3] = Tiles[x][z].Vertex[0].Shading;
			if(x!=0)
			{
			SS[x][z][3] += Tiles[x-1][z].Vertex[1].Shading;
			}
			if(z!=0)
			{
			SS[x][z][3] += Tiles[x][z-1].Vertex[3].Shading;
			}
			if(x!=0 && z!=0)
			{
			SS[x][z][3] += Tiles[x-1][z-1].Vertex[2].Shading;
			}
			//TOP RIGHT//

			SS[x][z][2] = Tiles[x][z].Vertex[1].Shading;
			if(z!=0)
			{
			SS[x][z][2] += Tiles[x][z-1].Vertex[2].Shading;
			}
			if(x!=199 && z!=0)
			{
			SS[x][z][2] += Tiles[x+1][z-1].Vertex[3].Shading;
			}
			if(x!=199)
			{
			SS[x][z][2] += Tiles[x+1][z].Vertex[0].Shading;
			}
			//BOTTOM RIGHT//

			SS[x][z][1] = Tiles[x][z].Vertex[2].Shading;
			if(x!=199)
			{
			SS[x][z][1] += Tiles[x+1][z].Vertex[3].Shading;
			}
			if(x!=199 && z!=199)
			{
			SS[x][z][1] += Tiles[x+1][z+1].Vertex[0].Shading;
			}
			if(z!=199)
			{
			SS[x][z][1] += Tiles[x][z+1].Vertex[1].Shading;
			}
			//BOTTOM LEFT//

			SS[x][z][0] = Tiles[x][z].Vertex[3].Shading;
			if(z!=199)
			{
			SS[x][z][0] += Tiles[x][z+1].Vertex[0].Shading;
			}
			if(x!=0 && z!=199)
			{
			SS[x][z][0] += Tiles[x-1][z+1].Vertex[1].Shading;
			}
			if(x!=0)
			{
			SS[x][z][0] += Tiles[x-1][z].Vertex[2].Shading;
			}
		}
	}
	for(x = 0; x < 200; x++)
	{
		for(int z = 0; z < 200; z++)
		{
			for(int v = 0; v < 4; v++)
			{
			Tiles[x][z].Vertex[v].Shading = SS[x][z][v]/4.0f;
			}
		}
	}
}
Now, that's a whole lot of data. Needless to say, I'm getting stack overflows. Now, having some idea of what to do, I tried the following code:
void BlendLighting()
{
	float *SS[200][200][4];
	for(int x = 0; x < 200; x++)
	{
		for(int z = 0; z < 200; z++)
		{
			//TOP LEFT//

			*SS[x][z][3] = Tiles[x][z].Vertex[0].Shading;
			if(x!=0)
			{
			*SS[x][z][3] += Tiles[x-1][z].Vertex[1].Shading;
			}
			if(z!=0)
			{
			*SS[x][z][3] += Tiles[x][z-1].Vertex[3].Shading;
			}
			if(x!=0 && z!=0)
			{
			*SS[x][z][3] += Tiles[x-1][z-1].Vertex[2].Shading;
			}
			//TOP RIGHT//

			*SS[x][z][2] = Tiles[x][z].Vertex[1].Shading;
			if(z!=0)
			{
			*SS[x][z][2] += Tiles[x][z-1].Vertex[2].Shading;
			}
			if(x!=199 && z!=0)
			{
			*SS[x][z][2] += Tiles[x+1][z-1].Vertex[3].Shading;
			}
			if(x!=199)
			{
			*SS[x][z][2] += Tiles[x+1][z].Vertex[0].Shading;
			}
			//BOTTOM RIGHT//

			*SS[x][z][1] = Tiles[x][z].Vertex[2].Shading;
			if(x!=199)
			{
			*SS[x][z][1] += Tiles[x+1][z].Vertex[3].Shading;
			}
			if(x!=199 && z!=199)
			{
			*SS[x][z][1] += Tiles[x+1][z+1].Vertex[0].Shading;
			}
			if(z!=199)
			{
			*SS[x][z][1] += Tiles[x][z+1].Vertex[1].Shading;
			}
			//BOTTOM LEFT//

			*SS[x][z][0] = Tiles[x][z].Vertex[3].Shading;
			if(z!=199)
			{
			*SS[x][z][0] += Tiles[x][z+1].Vertex[0].Shading;
			}
			if(x!=0 && z!=199)
			{
			*SS[x][z][0] += Tiles[x-1][z+1].Vertex[1].Shading;
			}
			if(x!=0)
			{
			*SS[x][z][0] += Tiles[x-1][z].Vertex[2].Shading;
			}
		}
	}
	for(x = 0; x < 200; x++)
	{
		for(int z = 0; z < 200; z++)
		{
			for(int v = 0; v < 4; v++)
			{
			Tiles[x][z].Vertex[v].Shading = *SS[x][z][v]/4.0f;
			}
		}
	}
}
Now, I get an "Unhandled exception in Lesson32.exe: 0xC0000005: Access Violation" error. This usually happens when I refer to an element of an array that does not exist- (For example: Dim a[5]; c = a[7]; ). However, I am quite certain that I am not attempting to pull data from an extraneous array member. Any tips? [edited by - T3hArtifex on May 8, 2004 1:29:22 PM]

Share this post


Link to post
Share on other sites
Advertisement
The reason you are getting stack overflows is because you're allocating 200*200*4*(4 bytes per float) = 640,000 bytes on the stack. Static arrays are always created on the stack. The stack memory limit is usually somewhere around a megabyte, but yours might be lower. It seems to me that you're calculating each vertex's shade by taking the average of the four values of the corresponding corners of the neighboring tiles, and then for each tile, you're averaging the four new corner values. By being a little redundant and doing some recalculations, you can save yourself all that memory and not need the SS array.

In your second piece of code, you're declaring an array of float pointers of the same size, but you're not initializing those pointers, so when you try to dereference them, you get an access violation error because they point to a random location in memory. If you want to allocate the array on the heap (which has a much larger memory limit than the stack), do something like:

int i, j;

float ***SS;

SS = new float**[200];

for(i = 0; i < 200; i++)
{
SS[i] = new float*[200];

for(j = 0; j < 200; j++)
SS[i][j] = new float[4];
}


And then at the end of the function, don't forget to delete everything, otherwise you'll have a very serious memory leak.

Oh yeah, and to get those nifty source boxes with syntax highlighting and space/tab preservation, put [source] and [/source] around your code.

[edited by - aprosenf on May 8, 2004 1:24:46 PM]

Share this post


Link to post
Share on other sites

  • Advertisement