C programming double free() prevention

typedef struct Node {
    struct Node* ptr;
    int value;


node* head = (node*)malloc(sizeof(node));
node* temp = head;


for (; head->ptr != NULL; head = temp) { //주소값으로 동적할당한 변수 free()
        temp = head->ptr;
        head = NULL;//double free() prevention
    free(temp); //last node free()
    temp = NULL;//double free() prevention

Is it right to use free() in the same way as above?

  • = NULL : double free() prevention? Is that possible?

위와 같은 방법으로 for문을 사용하여 free()를 써도 올바른 것인지 궁금합니다.
또한 NULL 값을 넣어주어 double free 를 방지하는 것이 가능한지 궁금합니다.

A free(NULL) is safe.

The assignment of head = NULL; inside the loop is pointless as head is immediately overwritten by the for logic.