*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/