循环第二次运行中的策略失败

发布于 2025-01-30 08:48:58 字数 781 浏览 4 评论 0原文

由于某种原因,我会遇到segmation Fail(核心转储),但仅在循环中的第二次运行中。某些背景PTR在main()中被声明为char*,并将其初始化为null。 附加代码和打印的屏幕截图

char* get_command(char **str) {
    char c;
    int i,add=0;
    printf("\tget command\n");

    for(i=0; (c=getchar())!= '\n';i++) {
        if(i==TOTAL_CHAR*add){
            *str = (char*)realloc(*str,sizeof(char)*TOTAL_CHAR*

    (++add));
    printf("\tmemory alocate succede\n");
        if (*str==NULL) {/*warning if realloc has failed*/
            fprintf(stderr,"ERROR:: realloc fail");
            exit(0);
        }
    }
    
    **(str+i)=c;
    printf("\tchar should be: %c, actual: %c\n",c,**(str+i));

    }
    return *str;
}

在此处输入图像描述

for some reason I get a segmation fail(core dump) but only on the second run in the loop. some background ptr is declared as char* in main() and initialized as NULL.
Attaching screenshot of the code and prints

char* get_command(char **str) {
    char c;
    int i,add=0;
    printf("\tget command\n");

    for(i=0; (c=getchar())!= '\n';i++) {
        if(i==TOTAL_CHAR*add){
            *str = (char*)realloc(*str,sizeof(char)*TOTAL_CHAR*

    (++add));
    printf("\tmemory alocate succede\n");
        if (*str==NULL) {/*warning if realloc has failed*/
            fprintf(stderr,"ERROR:: realloc fail");
            exit(0);
        }
    }
    
    **(str+i)=c;
    printf("\tchar should be: %c, actual: %c\n",c,**(str+i));

    }
    return *str;
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

朮生 2025-02-06 08:48:58

**(str+i)= c;它需要为*(*str+i)= c;

char* get_command(char **str)
{
    char c;
    int i,add=0;
    printf("\tget command\n");

    for(i=0; (c=getchar())!= '\n';i++)
    {
        *str = (char*)realloc(*str, i + 2);
        if (*str==NULL)
        {/*warning if realloc has failed*/
            fprintf(stderr,"ERROR:: realloc fail\n");
            free(*str);
            exit(0);
        }
    
        *(*str+i)=c;
        printf("\tchar should be: %c, actual: %c\n",c,*(*str+i));
            
    }
    *(*str + i) = 0;   
    return *str;
}

int main(void)
{
    char *s = NULL;

    get_command(&s);
    printf("string: \"%s\"\n", s);
}

https://godbolt.org/z/t4pmtjn3s

**(str+i)=c; it needs to be *(*str+i)=c;

char* get_command(char **str)
{
    char c;
    int i,add=0;
    printf("\tget command\n");

    for(i=0; (c=getchar())!= '\n';i++)
    {
        *str = (char*)realloc(*str, i + 2);
        if (*str==NULL)
        {/*warning if realloc has failed*/
            fprintf(stderr,"ERROR:: realloc fail\n");
            free(*str);
            exit(0);
        }
    
        *(*str+i)=c;
        printf("\tchar should be: %c, actual: %c\n",c,*(*str+i));
            
    }
    *(*str + i) = 0;   
    return *str;
}

int main(void)
{
    char *s = NULL;

    get_command(&s);
    printf("string: \"%s\"\n", s);
}

https://godbolt.org/z/T4PMTjn3s

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