M
mast2as
This question has been posted to this forum before and I read the
thread but found that the answers were perhaps imcomplete, so I am
trying again.
Whenever I am creating objects I would like to keep track of the
memory which is used for each object (for stats purpose). They are 2
basic approaches, one is to write a Memory manager type of class where
new & delete would be overloading the standard new & delete operator.
Each time the new member function of this class would be called I
could update a variable to would keep track of the bytes allocated by
new. This not completly satisafactory for me as this would be global
to the application.
Another way is to just recompute the size of the byte arrays and add
the result to a member variable. Something like that...
class A
{
private:
int *data;
float *floatData;
size_t mem;
public:
A( size_t arraySize, size_t arraySize2 )
{
mem = 0;
data = new int[ arraySize ];
dataFloat = new float[ arraySize2 ]
mem += sizeof( int ) * arraySize;
mem += sizeof( float ) * arraySize2;
}
}
I have read that 'new' reserves the memory which is needed for the
array of a certain type, plus some additional info at the beginning at
this array (which delete uses to know what is the size of the array it
needs to free). So instead of recompute the size of the array in bytes
and summing up the result to the variable mem is there a way one could
access the size of an array in bytes used by an array or created by
new ?
thanks -
thread but found that the answers were perhaps imcomplete, so I am
trying again.
Whenever I am creating objects I would like to keep track of the
memory which is used for each object (for stats purpose). They are 2
basic approaches, one is to write a Memory manager type of class where
new & delete would be overloading the standard new & delete operator.
Each time the new member function of this class would be called I
could update a variable to would keep track of the bytes allocated by
new. This not completly satisafactory for me as this would be global
to the application.
Another way is to just recompute the size of the byte arrays and add
the result to a member variable. Something like that...
class A
{
private:
int *data;
float *floatData;
size_t mem;
public:
A( size_t arraySize, size_t arraySize2 )
{
mem = 0;
data = new int[ arraySize ];
dataFloat = new float[ arraySize2 ]
mem += sizeof( int ) * arraySize;
mem += sizeof( float ) * arraySize2;
}
}
I have read that 'new' reserves the memory which is needed for the
array of a certain type, plus some additional info at the beginning at
this array (which delete uses to know what is the size of the array it
needs to free). So instead of recompute the size of the array in bytes
and summing up the result to the variable mem is there a way one could
access the size of an array in bytes used by an array or created by
new ?
thanks -