【发布时间】:2016-04-18 06:59:26
【问题描述】:
我怎样才能为 char** str 设置一个空值,因为我有“越界指针的取消引用:1 个字节(1 个元素)超过数组 im 使用 C 语言的结尾”的错误
while(part)
{
res = (char**)realloc(res, (i + 1) * sizeof(char*));
*(res + i) = mystrdup(part);
part = mystrdup(strtok(NULL, delim));
i++;
}
res = (char**)realloc(res, i * sizeof(char*));
*(res + i) = NULL; // This is where I Encounter the ERROR
【问题讨论】:
-
您的问题是否涉及 C++?怎么样?
-
在
realloc()之后,*(res + i) = NULL;不应该是*(res + i -1) = NULL;还是我错过了什么? -
“空值”是什么意思?值
0x0? 空指针? -
当我使用 *(res + i) = NULL 时,我得到了一个 Dereference of out of bound 的错误,但是当我使用 *(res + i -1) = NULL 如果我在 res 中的最后一个数据[2] = C 会变成空值,唯一保留的数据会在 res[0] = A, res[1] = B. 那么对于 res[3]=NULL,因为这个 *(res + i - 1) = NULL 将删除我的数组的最后一个值。这就是我使用它时得到的 *(res + i -1) = NULL
标签: c pointers dynamic-memory-allocation realloc off-by-one