0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct NBIG{
int numero;
struct NBIG *nseg;
struct NBIG *nant;
}Nbig;
int main(int argc,char *argv[])
{
char *nf,ch;
Nbig *vem=NULL,*resmulti=NULL,*dasoma=NULL;
Nbig *lista=NULL;
Nbig *lista2=NULL;
int cont,x,y;
FILE *arq;
if (argc>3) {
// ficheiro
for(cont=3; cont < argc; cont++){
nf=strcat(argv[cont], ".txt");
printf("%s",nf);
arq = fopen(nf, "r");
if(arq == NULL)
printf("Erro, nao foi possivel abrir o arquivo\n");
else{
fseek(arq, -1, SEEK_END);
long posicao = ftell(arq);
while(posicao >= 0){
fseek(arq, posicao, SEEK_SET);
fread(&ch, sizeof(char), 1, arq);
if (ch == ' ') {
dasoma=soma(dasoma, lista);
lista=apaga(lista);
vem=apaga(vem);
posicao=posicao-1;
}
else{
vem=makenode();
vem->numero= atoi(&ch);
lista=insertfirst(lista,vem);
posicao--;
}
}
printf("\n");
mostra(dasoma);
printf("\n");
fclose(arq);
}
}
//printf("%d Parametro: %s\n", cont,argv[cont]);
}
}
else{
...
}
In the file : 123 321
I do not understand why the code does not work. takes as 3rd argument a file name and returns this.
agrv(14077,0x7fff8f271380) malloc: * error for Object 0x7fd9f14026c0: Pointer being Freed was not allocated * set a breakpoint in malloc_error_break to debug Abort Trap: 6
The only function that uses the malloc and this
Nbig *makenode(){
Nbig *R = (Nbig*)malloc(sizeof(Nbig));
R->numero=0;
R->nant=NULL;
R->nseg=NULL;
return(R);
}
Nbig *apaga(Nbig *L){
while (L!=NULL) {
L=L->nseg;
free(L);
}
return L;
}
Nbig *insertfirst(Nbig *A,Nbig *nv){
if(A==NULL){
nv->nseg=NULL;
nv->nant=NULL;
return nv;
}
nv->nseg=A;
A->nant=nv;
return nv;
}
The error points to a free being called to an object that was not instantiated with malloc but in your code nowhere shows a free
– Penachia
only when you called the "delete" list function
– user48571