How to omit space to insert only numbers in different lists

Asked

Viewed 82 times

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

  • only when you called the "delete" list function

1 answer

0

The problem is in your job apaga.

In it you always delete the next non-null element (which can be null). Try to change the test to something similar to this:

Nbig *apaga(Nbig *L) {
  if (L == NULL)
      return NULL;
  while (L->nseg!=NULL) {
    L=L->nseg;
    free(L);
  }
  free(L);
  return NULL;
}
  • persists the same error

  • Debugging, where does the error occur? Still in the same place? I don’t have all the code to investigate for you...

  • is an if and Else on Else never enter pk step 3 arguments output: 1 21 321 agrv(28381,0x7fff8f271380) malloc: **error for Object 0x7f814bc026c0: inter being Freed was not allocated **set a breakpoint in malloc_error_break to debug

  • added more insertfirst function

  • Buddy, I don’t know what programming environment you’re using, but they all have debuggers. Use a debugger to run your code line by line, then see which line the problem is occurring on. Also note the value of variables before each statement - so you can see when unexpected values occur (possible causes of error). After that, tell me where the error is occurring - then I can help you more precisely.

  • it reads the file starts writing to the list and when it finds the space in the file does not execute it returns that problem must be in space, I do not know if it is of the function "atoi" that converts the character to integer

  • If you use a debugger, you can be sure it’s from the function atoi or not. However, I already say that if the error comes from this function, a undefined behavior (random behavior by invalid value) and not the error reported in the question statement.

  • how do I debug via finish

  • I see where the error is. The "come" list does not need to be deleted and when the position reaches -1 I have to add up to return the correct result.So just remove the line that deletes the list comes

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.