【发布时间】:2013-01-29 07:38:59
【问题描述】:
我必须安全地释放一个数组:char** a; 它就像一个字符串列表。我知道我有多少char*。但是我很难释放所有的内存。有没有我可以使用的函数来释放 20 个字节?我试过了:
for (int i = 0; i < length; i++)
if (a[i] != null)
free(a[i]); // some of a[i] ARE null, non-null have different sizes
free(a); // crashes here
但我在asm 调试时遇到运行时错误。
a 中的所有内容都已分配。对于一个 I malloced 5 个字符串(每个指针 4 个字节)-> 20 个字节。如何释放整个char**?
【问题讨论】:
-
您可能在其他地方有内存错误。尝试使用内存泄漏检测器(例如 Linux 上的
valgrind) -
什么运行时错误?他们都是
malloced