0

I'm trying to import information from a file to a struct, but when I'm using malloc the program stop running. I've done similar functions before, I don't know why is not working.

This is my reading file function:

void ler_fich_salas(List_sala sala)
{
 FILE *fich;
 List_sala l;
 char linha[10];
 fich = fopen("fich_salas.txt","r");
 l = l->next;
 if (fich == NULL)
 {
 return;
 }
 else
 {
 /*ou l=l->next*/
 while (!feof(fich))
 {
 printf("A");
 fgets(linha, 10, fich);
 printf("Z");
 printf("%s",linha);/*testar se le bem no fich*/
 printf("B");
 free(l->nome_sala);
 l->nome_sala = (char *)malloc(TAM*sizeof(char));
 printf("C");
 strcpy(l->nome_sala, strtok(linha,"\n"));
 printf("D");
 l = l->next;
 }
 }
 fclose(fich);
}

and this is my struct:

typedef struct Sala_node *List_sala;
typedef struct Sala_node
{
 char *nome_sala;
 List_sala next;
}Cada_sala;

Any help would be appreciated! Thanks in advance.

asked May 28, 2017 at 2:44
14
  • What is the value of TAM? Commented May 28, 2017 at 2:48
  • You do nothing with the sala parameter. Is that intentional? Commented May 28, 2017 at 2:48
  • I would guess that the call to free on an unallocated value is your problem. Commented May 28, 2017 at 2:49
  • 1
    Read about why you should not control file loops with feof(). Commented May 28, 2017 at 2:51
  • TAM = 1000. I should do with sala what I do with the l, but it was also doing an error. Also I called free because I thought that I was using too much memory, but I was just giving it a try. Commented May 28, 2017 at 2:58

1 Answer 1

1
List_sala l;
/* SNIP */
l = l->next;

From the use of the -> operator, l must be a pointer type, which means you're hiding pointer types behind a typedef, which is a bad idea.

Even more problematic is that you haven't assigned it to point at anything in your logic, and so the pointer value points at gibberish. It's a miracle your program runs up until the call to free, let alone the statement after it that you claim is problematic, as that's trying to deallocate more gibberish (something that wasn't allocated using malloc) and then (yet again) dereference and assign to even more gibberish.

Either you've cut the wrong parts of your logic out to form an MCVE, rendering our ability to reproduce the issue impossible without filling in blanks (don't do that) or... my guess is that your book isn't working for you, as people who read decent books don't tend to have this kind of misunderstanding; believe me, I've been there! Please see this list to find a better book.

answered May 28, 2017 at 2:58
Sign up to request clarification or add additional context in comments.

2 Comments

Thank's! I already realized what was my problem. In the main function I wasn't calling one function that create the list
No. Your most significant problem is that you gave us different code to what you used to reproduce the error. This kind of behaviour ruins the usefulness of most of your future questions. Please... In the future, make sure your MCVE allows others to reproduce the issue!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.