Reputation: 9
I made this method for a test and for some reason the malloc is not working. When I comment it it does work, but otherwise it will just say the program has stopped working.
typedef struct {
int NroUSP; // chave primária
int curso;
int estado;
int idade;
bool valido; // para exclusão lógica
} REGISTRO;
typedef struct s {
int NroUSP; // chave primária
int curso;
int estado;
int idade;
bool valido;
struct s* prox;
} NO;
typedef struct {
NO* inicio;
} LISTA;
These are the structures I used ^
EDIT: THE PROBLEM WAS THIS ASTERISK RIGHT BEFORE AUX
void ex6(){
REGISTRO* aux;
FILE *arq = fopen("C:\\Users\\jujuc_000\\Desktop\\Teste\\dados.bin","rb");
FILE *arq2 = fopen("C:\\Users\\jujuc_000\\Desktop\\Teste\\ex6.bin","wb");
LISTA l;
l.inicio = NULL;
NO*p = (NO*)malloc(sizeof(NO)); // this is the test malloc
if(arq){
while(1==fread(&aux,sizeof(REGISTRO),1,arq)){
/*p = (NO*)malloc(sizeof(NO)); // this is the one I want to keep
p->NroUSP = aux->NroUSP;
p->curso = aux->curso;
p->estado = aux->estado;
p->idade = aux->idade;
p->valido = aux->valido;
if(!l.inicio){
l.inicio = p;
}
p=p->prox;*/
}
}
fclose(arq);
fclose(arq2);
}
Upvotes: 0
Views: 66
Reputation: 157
Try the following:
Upvotes: 0
Reputation: 15009
fread(&aux,sizeof(REGISTRO),1,arq)
is most likely your problem; aux
is a pointer to a REGISTRO
, but you try to read in a full REGISTRO
into it, which more than likely overwrites memory, causing the apparent malloc
failure in the next line. Change the declaration to:
REGISTRO aux;
And things should work.
Upvotes: 1