Is this code fragment redundant?

0

Recently I did a C test where I had to complete a code to remove items from a queue, follow the code with the correct answer 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; , considering that immediately below I execute the command free(ptr_no); which will already release the memory allocated to struct ptr_no and its internal variables, is it redundant and unnecessary to undo the value? >     

asked by anonymous 14.08.2018 / 14:53

1 answer

1

Yes, it seems redundant, it is changing a data that is not used later on an object that will soon be destroyed.

Enjoying could have declared dado direct in its use and would have a less redundant code. And it could have used a type in typedef to avoid the redundancy of using struct .

typedef struct no {
    int dado;
    struct no *proximo;
} No;

typedef struct {
    No *inicio;
    No *fim;
} Fila;

I placed GitHub for future reference .

In the code you also no longer need to use struct .

    
14.08.2018 / 15:06