如果已知 void* 分配大小,如何将确切的内存大小传递给 free()
How to pass exact memory size to free() if void* allocation size is known
据我所知;在 c++ 和 c 中,delete
运算符或 free()
函数可以从 pionter 数据类型知道分配的内存大小,并且 delete
可以自动调用析构函数。
如果分配的指针是使用一种在其机制中不使用 new 运算符的单例静态动态数组注册的(即。仅 malloc
)
在重载的新指针中。这个数组注册 void*
指针并从重载 new operator
中获取该指针的大小
例如。
void * operator new (size_t sz) {
void * m = malloc (sz);
Dynamic_Array::get_instance()->add(m,sz) ; //registering pointer
return m ;
}
所以我的问题是如何使用有关大小的信息来释放我忘记使用此数组的析构函数删除的确切分配内存。
例如
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 ++;
}
}
谢谢。
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
没有。 delete操作符调用free()函数; free() 函数以某种方式从与指针关联的元数据中知道大小,例如内存前一个字中的长度字。
and delete can call the destructor automatically.
是的。
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.
你不需要知道。见上文。
在 C 中,
free() 函数使用围绕已分配内存的堆中的数据结构。
该数据结构包括分配内存的实际起始地址、堆中前一个内存块的地址和堆中下一个内存块的地址。
一般来说,堆其实就是两个链表。
一个是分配内存,一个是空闲内存。
malloc 操作从空闲链表中取出一个适当大小的块并将其放入已分配内存链表中。
这可能涉及将一个较大的空闲内存块分成两个较小的内存块,其中一个移动到分配的内存链表中,并在空闲内存链表中为新块调整指针
A free从分配的内存链表中移除表项,放入free链表中。
那么如果空闲链表包含两个相邻的内存块,则将这两个内存块组合成一个single/larger内存块。
有几种算法可用于确定哪个空闲区域被分配内存列表up/moved。
据我所知;在 c++ 和 c 中,delete
运算符或 free()
函数可以从 pionter 数据类型知道分配的内存大小,并且 delete
可以自动调用析构函数。
如果分配的指针是使用一种在其机制中不使用 new 运算符的单例静态动态数组注册的(即。仅 malloc
)
在重载的新指针中。这个数组注册 void*
指针并从重载 new operator
例如。
void * operator new (size_t sz) {
void * m = malloc (sz);
Dynamic_Array::get_instance()->add(m,sz) ; //registering pointer
return m ;
}
所以我的问题是如何使用有关大小的信息来释放我忘记使用此数组的析构函数删除的确切分配内存。
例如
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 ++;
}
}
谢谢。
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
没有。 delete操作符调用free()函数; free() 函数以某种方式从与指针关联的元数据中知道大小,例如内存前一个字中的长度字。
and delete can call the destructor automatically.
是的。
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.
你不需要知道。见上文。
在 C 中,
free() 函数使用围绕已分配内存的堆中的数据结构。
该数据结构包括分配内存的实际起始地址、堆中前一个内存块的地址和堆中下一个内存块的地址。
一般来说,堆其实就是两个链表。
一个是分配内存,一个是空闲内存。
malloc 操作从空闲链表中取出一个适当大小的块并将其放入已分配内存链表中。 这可能涉及将一个较大的空闲内存块分成两个较小的内存块,其中一个移动到分配的内存链表中,并在空闲内存链表中为新块调整指针
A free从分配的内存链表中移除表项,放入free链表中。
那么如果空闲链表包含两个相邻的内存块,则将这两个内存块组合成一个single/larger内存块。
有几种算法可用于确定哪个空闲区域被分配内存列表up/moved。