Row function in C

Asked

Viewed 178 times

0

I created the next 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 line up:

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 accuses in this function 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 is because I am not moving some part of the queue (I imagine). However I have already created the function of releasing the queue 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);


}
  • What error? Valgrind shows no errors, features memory usage patterns.

  • 1

    Are you sure you’re not bursting your memory in strcpy? If the CPF and the name include the null terminator in the appropriate position?

  • When I say error, I say that in relation to memory that is not being fully released. I have already checked the strcpy, the problem is not in it.

  • 1

    Without seeing how the rest of the code is hard to say anything. The ideal is to actually build a Minimum, Complete and Verifiable Example problem so that it is easy to reproduce the problem and indicate the solution

  • @Luiz Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

1


Valgrind has no way of stating what its code is doing for sure, if this were possible it would have already been incorporated into the compiler. This tool is an external aid that helps 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 actual errors. What it reports was allocated and not released in function enfileira(), but that doesn’t mean it wasn’t released elsewhere.

The function libera_fila() It frees up the memory of nodes that are null. I don’t know if it’s the right way and if it’s efficient, but it does this. It doesn’t release the knots that are still active. Valgrind hopes to work with a code ready for production, does not expect an exercise or simple code where you can allocate something and leave the operating system in charge. Valgrind wants you to release everything you have explicitly allocated. That’s why it appears that you have had 19 allocations and only 9 releases. 10 items must have stayed alive and never released. Even if they were not released during processing they should be released before closing the application, just to reset Valgrind. The other solution is not to use it or ignore these differences, which loses a little the reason to use it because then it becomes luck.

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

Browser other questions tagged

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