Queuing function in C

0

I've created the following queue:

typedef struct
No {
    int pos;
    char cpf[12];
    char nome[40];
    struct No *prox;
} No;
typedef struct No * p_no;

typedef struct{
    p_no ini, fim;
} Fila;
typedef Fila * p_fila;

And I have the following function to queue:

void enfileira(p_fila f, int pos, char cpf[], char nome[]) {
    p_no novo;
    novo = malloc(sizeof(No));
    novo->pos = pos;
    strcpy(novo->cpf, cpf);
    strcpy(novo->nome, nome);

    novo->prox = NULL;
    if(f->ini == NULL)
            f->ini = novo;
    else
            f->fim->prox = novo;
    f->fim = novo;
}

However, when I run Valgrind to check for memory errors, it says that some blocks are being lost:

==16485== HEAP SUMMARY:
==16485==     in use at exit: 600 bytes in 10 blocks
==16485==   total heap usage: 19 allocs, 9 frees, 6,036 bytes allocated
==16485== 
==16485== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 4
==16485==    at 0x482E27C: malloc (vg_replace_malloc.c:299)
==16485==    by 0x108709: enfileira (in /home/student/Downloads/lab06)
==16485==    by 0x108E83: main (in /home/student/Downloads/lab06)
==16485== 
==16485== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==16485==    at 0x482E27C: malloc (vg_replace_malloc.c:299)
==16485==    by 0x108709: enfileira (in /home/student/Downloads/lab06)
==16485==    by 0x109245: main (in /home/student/Downloads/lab06)

I understand that it's because I'm not dealing with some part of the queue (I imagine). But I've already created the queue function and called it in main() , but the error persists:

void libera_fila (p_fila f)
{
      p_no t;
      while(f->ini != NULL){
          t = f->ini;
          f->ini = f->ini->prox;
          free(t);
      }
      free(f);


}
    
asked by anonymous 18.10.2018 / 19:29

1 answer

1

Valgrind has no way of stating what its code is doing right, if this were possible it would have already been incorporated into the compiler. This tool is an external aid that helps to identify certain problems, but is not perfect. Nor is it guaranteed that all memory management errors will be detected and much less that some possible errors presented are in fact errors. What it informs you has been allocated and not released in the enfileira() function, but does not mean that it has not been released elsewhere.

The function libera_fila() frees the memory of nodes that are null. I do not know if it's the right way and if it's efficient, but she does it. It does not release the nodes that are still active. Valgrind expects to work with a production-ready code, does not expect an exercise or simple code where it can allocate something and let the operating system take care of it. Valgrind wants you to release everything you have explicitly allocated. So it appears that it had 19 allocations and only 9 releases. 10 items must have remained alive and never released. Even if they were not released during processing, they should be released before closing the application, just to clear Valgrind. The other solution is not to use it or ignore these differences, which loses a bit the reason to use it because it becomes lucky.

Anyway, it's just that it can be said with what was posted. It could even have some error, but in parts of the code that were not shown.

    
18.10.2018 / 19:51