Skip to main content
GameDev.net gamedev.net
🔒 Locked

fun with pointers (help)

Started by PDHB May 4, 2004 at 2:54 PM 4 replies 250+ views
Original Post
PDHB
PDHB
This is a function called from within a constructor to allocate memory that I wish to access via a 2d array (ie, a square matrix). AlphaMatrix is a pointer to pointers (an array of pointers). void BuildAlpha() { const int sizeAlpha = this->NumberOfParameters; const int sizeAlphaArray = this->NumberOfParameters * this->NumberOfParameters; // we dedicate sufficient memory to hold all of our alpha matrix float * tempAlpha = new float[sizeAlphaArray]; // we make a pointer to pointers this->AlphaMatrix = new float * [sizeAlpha]; // now we make our pointers point where they must for(int index = 0; index < sizeAlpha; index++) { this->AlphaMatrix[index] = tempAlpha + index * sizeAlpha; } } When this function ends, the pointer tempAlpha no longer exists. The memory is pointed to only through my Array of pointers. How can I then release all this memory when I am done with it? I know the simple answer is to just use managed code, but this is a time critical part of my applictation.
Behold the nascent power of Semejant!
Agony
Agony
Instead of using a temporary variable, couldn''t you just have another member variable, like AlphaMatrix? Then you would have the pointer available when you need to release the memory. It wouldn''t need to be a public member, obviously. Just a private one.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
PDHB
PDHB
I think this works fine for the exact case above, but I would also like to make a function to allocate memory this way any time I want to dynamically allocate a 2d array. I guess I could make a class or a struct that would act as a 2d array, but I prefer to avoid the extra overhead.
Behold the nascent power of Semejant!
PDHB
PDHB
Will this work?

this->AlphaMatrix = new float * [sizeAlpha];
for(int index = 0; index < sizeAlpha; index++)
{
this->AlphaMatrix[index] = new float[sizeAlpha];
}

and releasing the memory by releasing each element of AlphaMatrix?
Behold the nascent power of Semejant!
Agony
Agony
That wouldn't create a contiguous block of memory, however, which seems to be the point of what you were doing previously. I was just thinking though, that you could create your second array, the array of pointers, one size too large. You can then store the real pointer to the floats in that last element. In normal code, you'd never even notice it. And then when you delete, you would delete the real array, and then the array of pointers.
float** AlphaMatrix;
AlphaMatrix = new float*[sizeAlpha + 1];
AlphaMatrix[sizeAlpha] = new float[sizeAlphaArray];
for(int index = 0; index < sizeAlpha; index++)
{
AlphaMatrix[index] = AlphaMatrix[sizeAlpha] + index * sizeAlphaArray;
}
//

// Later on...

//

delete [] AlphaMatrix[sizeAlpha];
delete [] AlphaMatrix;
Does that look promising?

[Edit - forgot to make my array an array of pointers to float]

[edited by - Agony on May 4, 2004 5:16:21 PM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
PDHB
PDHB
clever.
Behold the nascent power of Semejant!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.