让我们声明一个指针
int *ptr;
ptr=malloc (10 *sizeof (int));
free (ptr);
问题是如何free()
将释放内存。让int
int
为 4 个字节,内存将为 40 个字节。
编译器将理解删除 malloc
提供的第一个内存位置。但是它如何清除另外 36 个字节的另外 9 个 int 数据呢?
最佳答案
malloc
留下 free
读取的注释。特别是,它留下了一条注释,说明分配了多少字节。
关于c++ - 在 C 或 C++ 中释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34556519/