0
I recently did a C proof in which I had to complete a code to remove items from a queue, follow the code with the answer considered correct highlighted:
typedef struct No {
int dado;
struct No *proximo;
};
typedef struct Fila {
struct No *inicio;
struct No *fim;
};
struct Fila *filaDePosts;
int filaRemover(){
struct No* ptr_no = fila->inicio;
int dado;
if (ptr_no != NULL) {
**fila->inicio = ptr_no->proximo;**
**ptr_no->proximo = NULL;**
dado = ptr_no->dado;
free(ptr_no);
return dado;
}
}
In the code snippet ptr_no->proximo = NULL;
, whereas just below I execute the command free(ptr_no);
which will already release the memory allocated to the struct ptr_no
and its internal variables, it is redundant and unnecessary to cancel the value?
Related or duplicate: Why assign NULL to a pointer after a free?
– Isac