Allocation/Deallocation from Memory Pool [Solved]
-
I am wondering if the type passed to delete[] needs to be the same as the type allocated by new? I am attempting to do the following:
void** memPoolPtrs = new void* [4 * (1 + 12)];
double* memPoolData = new double[4 * 12 * 30];
double*** myData = reinterpret_cast<double***>(memPoolPtrs);
memPoolPtrs += 4;
for (size_t idx0 = 0; idx0 < 4; ++idx0) {
myData[idx0] = reinterpret_cast<double**>(memPoolPtrs);
memPoolPtrs += 12;
for (size_t idx1 = 0; idx1 < 12; ++idx1) {
myData[idx0][idx1] = memPoolData;
memPoolData += 30;
}
}// Do some stuff with myData
delete[] **myData; // Delete ALL dynamically allocated doubles
delete[] myData; // Delete ALL dynamically allocated pointersIs there any risk in the above approach (other than allocating things of improper size)? I can't think of a reason why such a thing wouldn't work, but I am curious if the two delete statements will work as I anticipate. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
modified on Friday, April 16, 2010 12:41 PM
-
I am wondering if the type passed to delete[] needs to be the same as the type allocated by new? I am attempting to do the following:
void** memPoolPtrs = new void* [4 * (1 + 12)];
double* memPoolData = new double[4 * 12 * 30];
double*** myData = reinterpret_cast<double***>(memPoolPtrs);
memPoolPtrs += 4;
for (size_t idx0 = 0; idx0 < 4; ++idx0) {
myData[idx0] = reinterpret_cast<double**>(memPoolPtrs);
memPoolPtrs += 12;
for (size_t idx1 = 0; idx1 < 12; ++idx1) {
myData[idx0][idx1] = memPoolData;
memPoolData += 30;
}
}// Do some stuff with myData
delete[] **myData; // Delete ALL dynamically allocated doubles
delete[] myData; // Delete ALL dynamically allocated pointersIs there any risk in the above approach (other than allocating things of improper size)? I can't think of a reason why such a thing wouldn't work, but I am curious if the two delete statements will work as I anticipate. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
modified on Friday, April 16, 2010 12:41 PM
Since the
delete
operator takes avoid*
type, it doesn't matter what type is passed for deletion as long as it is the starting address of the allocation.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
I am wondering if the type passed to delete[] needs to be the same as the type allocated by new? I am attempting to do the following:
void** memPoolPtrs = new void* [4 * (1 + 12)];
double* memPoolData = new double[4 * 12 * 30];
double*** myData = reinterpret_cast<double***>(memPoolPtrs);
memPoolPtrs += 4;
for (size_t idx0 = 0; idx0 < 4; ++idx0) {
myData[idx0] = reinterpret_cast<double**>(memPoolPtrs);
memPoolPtrs += 12;
for (size_t idx1 = 0; idx1 < 12; ++idx1) {
myData[idx0][idx1] = memPoolData;
memPoolData += 30;
}
}// Do some stuff with myData
delete[] **myData; // Delete ALL dynamically allocated doubles
delete[] myData; // Delete ALL dynamically allocated pointersIs there any risk in the above approach (other than allocating things of improper size)? I can't think of a reason why such a thing wouldn't work, but I am curious if the two delete statements will work as I anticipate. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
modified on Friday, April 16, 2010 12:41 PM
For yor case it works. As a general rule it won't. delete[] against an object, whose type has a destructor will call the 'vector deleting destructor', which will call the destructor for each element. The problem is the number of elements. When new[] is called on a type with a destructor: 1. malloc a block large enough to hold the reqested # of elements, the actual block size will be rounded up to some grain size. 2. store the actual block size (bytes) with the block for free(). 3. store the requested # of elements with the block so delete[] can iterate over them and call ~(). When you call delete on a different (sized) type the # of elements stored with the block will be wrong and you will either call ~() on too few or too many elements. If too few then you could have a memory leak if the destructor frees resources. If too many then you corrupt the heap.
...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack
-
I am wondering if the type passed to delete[] needs to be the same as the type allocated by new? I am attempting to do the following:
void** memPoolPtrs = new void* [4 * (1 + 12)];
double* memPoolData = new double[4 * 12 * 30];
double*** myData = reinterpret_cast<double***>(memPoolPtrs);
memPoolPtrs += 4;
for (size_t idx0 = 0; idx0 < 4; ++idx0) {
myData[idx0] = reinterpret_cast<double**>(memPoolPtrs);
memPoolPtrs += 12;
for (size_t idx1 = 0; idx1 < 12; ++idx1) {
myData[idx0][idx1] = memPoolData;
memPoolData += 30;
}
}// Do some stuff with myData
delete[] **myData; // Delete ALL dynamically allocated doubles
delete[] myData; // Delete ALL dynamically allocated pointersIs there any risk in the above approach (other than allocating things of improper size)? I can't think of a reason why such a thing wouldn't work, but I am curious if the two delete statements will work as I anticipate. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
modified on Friday, April 16, 2010 12:41 PM