我尝试使用Realloc函数分配并获取错误
typedef struct{
char** strings_cmd;
int size_cmd;
}parseInfo;
....
parseInfo* parse(char* cmd){
char* temp = strdup(cmd);
char* temp_split = strtok(temp," ");
int i = 0;
char** strings = (char**)malloc(sizeof(char*));
if(strings == NULL){
printf("no memory allocated strings parse()\n");
exit(1);
}
while(temp_split != NULL){
strings[i++] = strdup(temp_split);
strings = realloc(strings,i * sizeof(char*));
if(strings == NULL){
printf("no memory allocated strings (while) parse()\n");
exit(1);
}
temp_split = strtok(NULL," ");
}
strings[i] = NULL;
parseInfo* info = (parseInfo*)malloc(sizeof(parseInfo));
if(info == NULL){
printf("no memory allocated info parse()\n");
exit(1);
}
info->strings_cmd = strings;
info->size_cmd = i;
return info;
}
大家好,我收到错误:
realloc():下一个大小无效。
我尝试做的是输入一个字符串并将其分解为单词 例如我输入=“Hello World”。 并分割它=“Hello”,“World”,
但是当我传递4个单词时,我得到了这个错误......
typedef struct{
char** strings_cmd;
int size_cmd;
}parseInfo;
....
parseInfo* parse(char* cmd){
char* temp = strdup(cmd);
char* temp_split = strtok(temp," ");
int i = 0;
char** strings = (char**)malloc(sizeof(char*));
if(strings == NULL){
printf("no memory allocated strings parse()\n");
exit(1);
}
while(temp_split != NULL){
strings[i++] = strdup(temp_split);
strings = realloc(strings,i * sizeof(char*));
if(strings == NULL){
printf("no memory allocated strings (while) parse()\n");
exit(1);
}
temp_split = strtok(NULL," ");
}
strings[i] = NULL;
parseInfo* info = (parseInfo*)malloc(sizeof(parseInfo));
if(info == NULL){
printf("no memory allocated info parse()\n");
exit(1);
}
info->strings_cmd = strings;
info->size_cmd = i;
return info;
}
hello guys i get the error:
realloc(): invalid next size.
and what i try to do is to input a string and split it down into words
for example i input = "Hello World".
and to split it = "Hello" , "World"
but when i pass 4 words i got this error...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于初学者来说,该函数存在内存泄漏,因为在函数的开头分配了
未释放的内存。
在这个 while 循环中,
您需要编写
为该语句中使用的终止空指针保留一个元素
,并且您需要在函数开头释放动态分配的内存,就像
您分配一个少一个元素的指针数组一样这是必需的。
For starters the function has a memory leak because in the beginning of the function there is allocated memory
that was not freed.
In this while loop
You need to wirte
to reserve one element for the terminating null pointer used in this statement
And you will need to free the allocated dynamically memory in the beginning of the function like
you are allocating an array of pointers with one less element that it is required.
这行代码很糟糕:
这行代码将数组大小调整为
i
元素。然后,在下一次迭代中,某个值被存储到数组的第 i 个元素(由
strings
指向)。该数组只有i
个元素(0
到i-1
),因此这是超出范围的访问。分配足够的元素来修复:
另请注意
malloc()
系列的转换结果为 被认为是一种不好的做法。This line is bad:
This line is resizing the array to
i
elements.Then, in the next iteration, some value is stored to the
i
-th element of the array (pointed at by)strings
. The array has onlyi
elements (0
toi-1
), so this is out-of-range access.Allocate enough elements to fix:
Also note that casting results of
malloc()
family is considered as a bad practice.