dimanche 8 mars 2015

How to pass exact memory size to free() if void* allocation size is known


As far as I know ; In c++ and c delete operator or free() function can know the size of allocated memory from pionter data type and delete can call the destructor automatically.


If the allocated pointer is registered using a kind of singleton static dynamic array which don't use new operator in its mechanism (ie. malloc only) in oveloaded new pointer. this array register void* pointers and take the size of that pointer from overloaded new operator


Ex.



void * operator new (size_t sz) {
void * m = malloc (sz);
Dynamic_Array::get_instance()->add(m,sz) ; //registering pointer
return m ;
}


so My question is how to use the information about size to release the exact allocated memory that I forget to delete using the destructor of this array.


Ex.



Dynamic_Array::~Dynamic_Array() {
int index = 0;
while(index < storage_size) //storage_size: total number of pointers in array

/* storage is void** array which register
allocated pointers as void* and use classic memset and memcpy to enlarge*/

{
if (storage[index] != NULL) printf("Pointer : %d was not deleted\n", storage[index]);
//how to use delete or free here to delete void* pointer but with known size of memory
index ++;
}
}


Thank you.




Aucun commentaire:

Enregistrer un commentaire