首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在c中获取“损坏的大小与prev_size”错误

在c中获取“损坏的大小与prev_size”错误
EN

Stack Overflow用户
提问于 2022-04-24 16:19:39
回答 2查看 480关注 0票数 1

我需要做一个功能,以接收一个无限的输入,这将停止阅读时,达到和EOF。我必须使用realloc函数。

这是我目前的代码:

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>

char *getInput() {
    char *currentString, currentChar;
    int currentSize;
    currentString = (char *)malloc(sizeof(char) * 2);
    currentSize = 0;
    currentChar = 0;
    if (currentString != NULL) {
        while (currentChar != EOF) {
            currentChar = getchar();
            currentString[currentSize++] = currentChar;
            currentString = realloc(currentString, currentSize);
        }
    }
    return currentString;
}

int main(int argc, char *argv[]) {
    char *userInput;

    userInput = getInput();
    printf("\n string is: %s", userInput);
    return 0;
}

代码正常工作,除非我输入的字符串大小超过13个字符,否则会出现以下错误(文件名为list_ab):

代码语言:javascript
运行
复制
*** Error in `./list_ab': corrupted size vs. prev_size: 0x08ff6010 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb75a7377]
/lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb75ad2f7]
/lib/i386-linux-gnu/libc.so.6(+0x7049e)[0xb75b049e]
/lib/i386-linux-gnu/libc.so.6(realloc+0x10e)[0xb75b162e]
./list_ab[0x804851d]
./list_ab[0x8048544]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb7558637]
./list_ab[0x80483d1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:02 359220     /home/user/Desktop/Maman12/list_ab
08049000-0804a000 r--p 00000000 08:02 359220     /home/user/Desktop/Maman12/list_ab
0804a000-0804b000 rw-p 00001000 08:02 359220     /home/user/Desktop/Maman12/list_ab
08ff6000-09017000 rw-p 00000000 00:00 0          [heap]
b7400000-b7421000 rw-p 00000000 00:00 0 
b7421000-b7500000 ---p 00000000 00:00 0 
b750d000-b7529000 r-xp 00000000 08:02 931606     /lib/i386-linux-gnu/libgcc_s.so.1
b7529000-b752a000 rw-p 0001b000 08:02 931606     /lib/i386-linux-gnu/libgcc_s.so.1
b7540000-b76f0000 r-xp 00000000 08:02 933356     /lib/i386-linux-gnu/libc-2.23.so
b76f0000-b76f2000 r--p 001af000 08:02 933356     /lib/i386-linux-gnu/libc-2.23.so
b76f2000-b76f3000 rw-p 001b1000 08:02 933356     /lib/i386-linux-gnu/libc-2.23.so
b76f3000-b76f6000 rw-p 00000000 00:00 0 
b770b000-b770e000 rw-p 00000000 00:00 0 
b770e000-b7710000 r--p 00000000 00:00 0          [vvar]
b7710000-b7711000 r-xp 00000000 00:00 0          [vdso]
b7711000-b7733000 r-xp 00000000 08:02 931813     /lib/i386-linux-gnu/ld-2.23.so
b7733000-b7734000 rw-p 00000000 00:00 0 
b7734000-b7735000 r--p 00022000 08:02 931813     /lib/i386-linux-gnu/ld-2.23.so
b7735000-b7736000 rw-p 00023000 08:02 931813     /lib/i386-linux-gnu/ld-2.23.so
bfd35000-bfd56000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

在大量搜索之后,我发现每当我试图写到一个无法访问的位置时,错误就会发生,但是上面提到的修复都没有效果。我怎样才能解决这个问题?如果它有助于在linux上运行。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-24 17:01:19

代码中存在多个问题:

(-1)).类型currentChar的类型应该是int,以适应getchar()返回的所有可能的值,即unsigned char类型的所有值(8位字节的0255 )和特殊的负值EOF (通常定义为getchar())。

  • 第一次测试currentChar != EOF时,变量currentChar未初始化,导致未定义的行为。

  • 您应该在从标准输入读取字节之后并在将其附加到数组.

之前测试EOF

  • --在设置下一个字符之后重新分配数组:除了第一次将字节写入到分配对象的末尾以外:您应该在追加字节之前重新分配数组

  • 您没有为空终止符分配足够的空间,也没有设置它。

  • 您不检查malloc()realloc()失败。

main().中没有释放字符串的

  • reallocating一次一个字节是inefficient.

以下是修改后的版本:

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>

char *getInput(void) {
    size_t len = 0;
    size_t size = 8;
    char *str = malloc(size);
    char *newstr;
    int c;

    if (str == NULL) {
        fprintf(stderr, "malloc failure for %zu bytes\n", size);
        return NULL;
    }
    while ((c = getchar()) != EOF) {
        if (len + 2 > size) {
            /* increase the allocated block size by 50% */
            size += size / 2;
            newstr = realloc(str, size);
            if (newstr == NULL) {
                free(str);
                fprintf(stderr, "realloc failure for %zu bytes\n", size);
                return NULL;
            }
            str = newstr;
        }
        str[len++] = c;
    }
    /* try and reduce the size of the allocated string */
    newstr = realloc(str, len + 1);
    if (newstr != NULL)
        str = newstr;
    /* set the null terminator */
    str[len] = '\0';
    return str;
}

int main() {
    char *userInput = getInput();
    if (userInput) {
        printf("string is: %s\n", userInput);
        free(userInput);
    }
    return 0;
}
票数 1
EN

Stack Overflow用户

发布于 2022-04-24 16:58:53

总结评论中的所有意见:

不使用

  1. argc & argv,那么为什么声明them?
  2. getchar()返回一个int来覆盖通常设置为1的所有位在2的补码表示法中成为-1。因此,需要为int.
  3. String的currentChar不是null终止的,在使用后作为以nul结束的内存使用时可能会导致缓冲区溢出。
  4. str中为nul('\0')留出空间&检查realloc().
  5. Don't的返回值是否慷慨,使用变量名,读取将花费时间。

简化代码:

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>

char* getInput() {
    char *str = malloc (sizeof (char) * 2);
    if (!str) {
        perror ("malloc"); return NULL;
    }
    int ich;
    int slen = 0;
    while ((ich = getchar()) != EOF) {
        str[slen++] = ich;
        char* str2 = realloc (str, slen + 1);
        if (!str2) {
            perror ("realloc"); free (str); return NULL;
        }
        str = str2;
    }
    str[slen] = '\0';
    return str;
}

int main() {
    char *userInput = getInput();
    if (userInput) {
        printf ("String is: %s\n", userInput);
        free (userInput);
    }
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71990243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档