奇怪的段错误,可能与 realloc [英] strange seg fault, likely with realloc

查看:17
本文介绍了奇怪的段错误,可能与 realloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char *dumpTB (TB tb){

    char* text = malloc(sizeof(char));

    int i = 0; // 
    int x = 0; //string index

    tNode* curr = tb->head;

    while(curr != NULL){

        while(curr->line[x] != '
'){
            printf("%d", i);
            text[i] = curr->line[x];
            printf("%c
", text[i]);

            text = realloc(text, i+1);

            i++;
            x++;
        }
        text[i] = '
';
        printf("%c", text[i]);
        text = realloc(text, i+1); 
        i++;

        x = 0; 
        curr = curr->next; 
    }

    return text;
}

所以我设法使用打印语句打印出字符串的前 12 个字母,但由于某种原因,它在打印第 12 个字母l"后不久给了我一个段错误,并且根据打印语句,它似乎发生了在 realloc 周围...谁能告诉我我做错了什么?

So I manage to print out the first 12 letters of my string using the print statements but for some reason it gives me a seg fault shortly after printing the 12th letter 'l', and based on the print statements it seems to occur around the realloc...can anyone tell me what I did wrong?

int i = 1; // 
    int x = 0; //string index

    tNode* curr = tb->head;

    while(curr != NULL){

        while(curr->line[x] != '
'){
            printf("%d", i-1);
            text[i-1] = curr->line[x];
            printf("%c
", text[i-1]);

            text = realloc(text, i+1);
            i++;
            x++;
        }
        printf("%d
", i-1);
        text[i-1] = '
';
        printf("%c", text[i-1]);
        text = realloc(text, i+1); 
        i++;

        x = 0; 
        curr = curr->next; 
        //printf("%c
", curr->line[0]);
    }

我尝试修复索引错误,这是一个看起来很长的 sysmalloc 断言,它会中止程序.

I tried fixed the index errors, a really long looking sysmalloc assertion thing which aborts the program.

推荐答案

你可能想要 getline(3),所以如果你有它使用它.正如 AnT 的答案 所解释的,你有 未定义行为 (UB),因为 缓冲区溢出一个>.尽快修复它.非常害怕 UB,并花更多时间阅读它(这很棘手).

You probably want getline(3), so use it if you have it. As explained by AnT's answer, you have undefined behavior (UB) because of a buffer overflow. Fix that as soon as possible. Be very scared of UB and take more time to read about it (it is tricky).

另外,请记住 malloc(3)realloc(3) 在某种程度上是昂贵的调用(对于某些合适的昂贵概念;实际上,它们非常快 - 在桌面上合理使用通常不到一微秒),并且它们可能会失败(通过给出 NULL)并且你应该 检查那个.

Also, remember that both malloc(3) and realloc(3) are somehow expensive calls (for some suitable notion of expensive; actually, they are quite fast - often less than a microsecond on a desktop for reasonable use), and they could fail (by giving NULL) and you should check that.

在实践中,您最好少使用 realloc.根据经验,您希望将使用的长度和分配的大小都保留在某个地方(例如,在其他局部变量中),并且您可能会使用一些几何级数(例如 newsize = 4*oldsize/3 + 10....) 以避免过于频繁的 realloc-s.为每个额外的字节做一个 realloc 是丑陋的.

In practice, you'll better use realloc less often. As a rule of thumb, you want to keep both the used length and the allocated size somewhere (e.g. in additional local variables), and you might use some geometrical progression (e.g. newsize = 4*oldsize/3 + 10....) to avoid too frequent realloc-s. Doing a realloc for each additional byte is ugly.

使用所有警告和调试信息编译您的代码,例如gcc -Wall -Wextra -gGCC.改进代码以获得没有警告.使用调试器 gdbvalgrind 寻找 内存泄漏 和其他麻烦.

Compile your code with all warnings and debug info, e.g. gcc -Wall -Wextra -g with GCC. Improve the code to get no warnings. Use the debugger gdb and valgrind to hunt memory leaks and other trouble.

这篇关于奇怪的段错误,可能与 realloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆