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.
– Maniero
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?– Victor Stafusa
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.
– Luiz
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
– Isac
@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
– Maniero