【问题标题】:Compiler complain about ''*** glibc detected *** ./myprog: corrupted double-linked list: 0x0000000001cd6240 *** ''编译器抱怨''*** glibc检测到*** ./myprog:损坏的双链表:0x0000000001cd6240 ***''
【发布时间】:2015-12-02 21:04:21
【问题描述】:

我有这个功能:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct adress Adress;

struct adress {char *num; char *street; char *namestreet; char *city; char *postal;};

int main(void) {

    int n = 0;

    int i;

    struct adress *Adress;

    FILE *f = fopen("/home/file.txt","r");
    if (!f) {
        printf("Impossible to open file\n");
        return 1;
    }
    char line[256];
    while (fgets(line,256,f)) {
        if (n == 0) {
            Adress = malloc(sizeof(struct adress)*(n+1));
        }
        Adress[n].num = malloc( 256*sizeof( char));

        Adress[n].street = malloc( 256*sizeof( char));

        Adress[n].namestreet = malloc( 256*sizeof( char));

        Adress[n].city = malloc( 256*sizeof( char));

        Adress[n].postal = malloc( 256*sizeof( char));

        fscanf( f , "%s %s %s %s %s", Adress[n].num, Adress[n].street, Adress[n].namestreet, Adress[n].city ,Adress[n].postal);

        n++;  
    }

    for (i = 0; i < n; i++)
    {
        printf("\nNum : %s \n", Adress[i].num);
        printf("\nStreet : %s \n", Adress[i].street);
        printf("\nStreet Name :%s \n", Adress[i].namestreet);
        printf("\nCity :%s \n", Adress[i].city);
        printf("\nPostal Code : %s \n", Adress[i].postal);
    }
    fclose(f);
    return 0;
}

当我尝试编译我的程序时,出现以下错误:

* 检测到 glibc * ./myprog: 损坏的双链表:0x0000000001cd6240

我做错了什么?

以前有人遇到过这种错误吗?

【问题讨论】:

  • 对于 typedef 和本地数组指针的名称使用相同的标识符 Adress 是相当混乱的。
  • Adress 只有一个元素 by Adress = malloc(sizeof(struct adress)*(n+1));
  • 澄清一下,不是编译器提供输出,而是你的程序。
  • 我该怎么做才能导致问题?

标签: c


【解决方案1】:

你只为一个struct adress分配了足够的空间,如果你读了多行,你会写到Adress指向的数组的末尾之外并破坏堆。随后对malloc 的调用检测到这一点并中止并显示消息:./myprog: corrupted double-linked list: 0x0000000001cd6240

Adress 初始化为NULL

    struct adress *Adress = NULL;

并更改这 3 行:

    if (n == 0) {
        Adress = malloc(sizeof(struct adress)*(n+1));
    }

    Adress = realloc(Adress, sizeof(struct adress)*(n+1));
    if (!Adress) {
         printf("out of memory\n");
         exit(1);
    }

【讨论】:

    【解决方案2】:

    您的程序有问题并且损坏了处理您的内存的 libc 链表。

    lib c 将“私有”数据存储在您分配的大块内存中。

    通常会有这样的标题:

    struct hdr
    {
      size_t size;                  /* Exact size requested by user.  */
      unsigned long int magic;      /* Magic number to check header integrity.  */
      struct hdr *prev;
      struct hdr *next;
      __ptr_t block;                /* Real block allocated, for memalign.  */
      unsigned long int magic2;     /* Extra, keeps us doubleword aligned.  */
    };
    

    如果你的代码中的缓冲区溢出破坏了其中一个魔法,你会得到这样的错误(因为 libc 可以发现它的结构被破坏了)

    【讨论】:

    • 恐怕你会让 OP 更加困惑。堆管理内部超出了这个问题的范围。帮助 OP 找到有问题的代码更有用。
    • 非常感谢 ;) 现在程序没有错误,但是当我在文件中放三个 o 表示地址时,输出中只有两个地址。为什么??
    猜你喜欢
    • 2013-10-05
    • 2013-11-01
    • 2013-12-05
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多