我尝试使用Realloc函数分配并获取错误

发布于 2025-01-18 13:54:37 字数 1148 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2025-01-25 13:54:37

对于初学者来说,该函数存在内存泄漏,因为在函数的开头分配了

parseInfo* parse(char* cmd){
    char* temp = strdup(cmd);
    //...

未释放的内存。

在这个 while 循环中,

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 = realloc(strings, ( i + 1 ) * sizeof(char*));

为该语句中使用的终止空指针保留一个元素

strings[i] = NULL;

,并且您需要在函数开头释放动态分配的内存,就像

free( temp );

}

您分配一个少一个元素的指针数组一样这是必需的。

For starters the function has a memory leak because in the beginning of the function there is allocated memory

parseInfo* parse(char* cmd){
    char* temp = strdup(cmd);
    //...

that was not freed.

In this while loop

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," ");

You need to wirte

strings = realloc(strings, ( i + 1 ) * sizeof(char*));

to reserve one element for the terminating null pointer used in this statement

strings[i] = NULL;

And you will need to free the allocated dynamically memory in the beginning of the function like

free( temp );

}

you are allocating an array of pointers with one less element that it is required.

蓝咒 2025-01-25 13:54:37

这行代码很糟糕:

        strings = realloc(strings,i * sizeof(char*));

这行代码将数组大小调整为 i 元素。
然后,在下一次迭代中,某个值被存储到数组的第 i 个元素(由 strings 指向)。该数组只有 i 个元素(0i-1),因此这是超出范围的访问。

分配足够的元素来修复:

        strings = realloc(strings,(i + 1) * sizeof(char*));

另请注意 malloc() 系列的转换结果为 被认为是一种不好的做法

This line is bad:

        strings = realloc(strings,i * sizeof(char*));

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 only i elements (0 to i-1), so this is out-of-range access.

Allocate enough elements to fix:

        strings = realloc(strings,(i + 1) * sizeof(char*));

Also note that casting results of malloc() family is considered as a bad practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文