Strange issue with pointers (i think)

Started by
3 comments, last by Kustaz 18 years ago
Hi, I've been trying to get the following block of code to work, but for some reason, it doesnt even appear to recognise anything I do to it inside the ReduceMesh() function. I've torn my hair out for a while about it, and would appreciate any help you guys could give me. The function looks like this:


inline UINT ReduceMesh(PRECISION **OriginalMesh, UINT Dims, PRECISION **NewMesh, UINT Factor)
{

	UINT Size = (Dims + 1) / Factor;

	NewMesh = new PRECISION*[Size];

		for(UINT Cycle = 0; Cycle < Size; Cycle++)
		{

			NewMesh[Cycle] = new PRECISION[Size];

				for(UINT nCycle = 0; nCycle < Size; nCycle++)
				{
				NewMesh[Cycle][nCycle] = 0;
				}

		}
			


		for(UINT Y = 0; Y < Size; Y++)
		{
			
			for(UINT X = 0; X < Size; X++)
			{



				NewMesh[Y][X] = OriginalMesh[Y * Factor][X * Factor];




			}


		}


return Size;
}


Dims is the size of the original mesh, and Factor is how much to reduce the mesh by. a value of 2 means it will have half as much detail. and the call to it looks like this:

ReduceMesh(VertexData, 33, TEST, 2);

VertexData is a valid array, and i have tested it to make sure. The decleration for TEST looks like this:

PRECISION **TEST = NULL;

Please not that PRECISION IS JUST A FLOAT. Iv got it set like this so i can easily change how precise the engine is. Thanks once again for any help you can give me, if there are any other bits of information I've left out that could be causing problems, please let me know. Cheers, Kustaz.
Advertisement
Other issues aside, your problem may be that you're passing NewMesh by value rather than by (non-constant) reference.
Oh ok, thanks, how would I go about fixing it so it does pass by reference?

And please, let fly with the other issues, but please be constructive about them (not just criticise for the sake of criticising).
Quote:Original post by Kustaz
Oh ok, thanks, how would I go about fixing it so it does pass by reference?
inline UINT ReduceMesh(PRECISION **OriginalMesh, UINT Dims, PRECISION **&NewMesh, UINT Factor)
That does look kind of weird, but if I got it wrong I'm sure someone will correct it.
Quote:And please, let fly with the other issues, but please be constructive about them (not just criticise for the sake of criticising).
Really just the usual admonition to take advantage of the C++ standard library, especially the container classes. Manual memory management usually isn't necessary in C++, and tends to be messy, error-prone, and hard to maintain.

In the nitpicky (but constructive, I hope!) category:

1. Use a typedef or template parameter for the scalar type, rather than a macro (I assume PRECISION is a #define)
2. You might consider a different name than 'precision', such as 'scalar' or 'real'
3. Maybe use the more conventional 'i' and 'j' instead of the 'cycle' variable names
Thanks so much for the help Jyk, it now works. The 'PRECISION' is actually a typedef. I'l take your other points into considertion, and look at revising the code to remove the messy manual memory management stuff. Thanks alot for your help, it's much appreciated.

This topic is closed to new replies.

Advertisement