【发布时间】:2015-06-17 04:41:39
【问题描述】:
我刚刚开始学习 malloc'd 和 realloc'd 数组。谁能帮我解释一下如何正确释放我的以下数组?我曾尝试查看其他帖子,但我很难理解 C 中的内存分配。
char ** result = NULL;
int numSpaces = 0;
char * p = strtok(command, " ");
/* split string and append tokens to 'result' */
while (p)
{
result = realloc (result, sizeof (char*) * ++numSpaces);
if (result == NULL)
exit (-1); /* memory allocation failed */
result[numSpaces-1] = p;
p = strtok(NULL, " ");
}
【问题讨论】: