c - 重新分配无效指针。中止(核心转储)

标签 c pointers realloc

     *MyFile.h*

        typedef char* dado_t;
        typedef struct elemento elemento;
        typedef struct lista2 lista2;  

    *MyFile.c*

    struct elemento{
        dado_t str;
        elemento* ant;
        elemento* prox;
    };

    struct lista2{
        elemento* primeiro;
        elemento* ultimo;
        elemento* corrente;
    };

    void_insert(lista2* l, dado_t d){
       elemento* novo = malloc(sizeof(elemento*));
       novo->str = malloc(strlen(d) * sizeof(char));
       novo->str = d;
       l->corrente = novo;
       l->primeiro = novo;
       l->ultimo = novo;
       novo->prox = NULL;
       novo->ant = NULL;

    }

dado_t replace(lista2* l, dado_t d){
   dado_t retorno = malloc(strlen(corrente->str) * sizeof(dado_t));
   retorno = corrente->str;

   corrente->str = realloc(corrente->str, strlen(d) * sizeof(char));
   l->corrente->str = d;
   return retorno;
}

为什么我会收到这个错误?因为 myel->str 是一个用 malloc() 分配的指针。为什么错误?如果 realloc()

发生错误,我正在使用一个临时元素*来阻止

观测:

最佳答案

使用 elemento* novo = malloc(sizeof(elemento*)); 您 malloc 仅为指针分配空间,然后将其视为结构空间。这会覆盖堆上的簿记信息。

关于c - 重新分配无效指针。中止(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30041817/

相关文章:

c - 添加到前缀树时出现段错误

c 指针 双指针

c - 我如何打印一个带有 void 函数的指针?

c# - 差异 b/n 对象、引用、指针

c - 双指针出现段错误

c - C prime 生成器中 realloc 的看似未定义的行为(程序已发布,可运行)

c - 使用 realloc 增加数组的大小

c - 操作系统导致的 irq 延迟是多少?

c++ - 动态内存分配被覆盖?

c - 调整可能太大的字符串数组的大小