Assigning a pointer after it is released

I am looking at the following code from Interviewing Programming:

bool deleteStack( Element **stack ){
      Element *next;
      while( *stack ){
            next = (*stack)->next;
            free( *stack );
            *stack = next;
      }
      return true;
}

I am not familiar with C ++ or C, so this might be a dumb question, but would not assign something to the pointer after release, will this cause a problem?

+5
source share
4 answers

In your example *stack, this is a pointer. It is completely safe to free the memory that it points to, and then assign a pointer to a new variable.

The only thing that would be unsafe would be dereferencing *stackafter his release.

free( *stack );
next = (*stack)->next;

, , *stack, ( ) free.

+8

, . " ":

 free( *stack );

, *stack. :

*stack = next;

, .

+4

. - (*stack), " ". free , " ", . , (.. ) , .
, , , , , ->, C/++, . , : stack->next : (*stack).next .

+1

NULL - , , .

NULL , undefined ( 50%), . NULL, . , , .

, , . , ( ).

+1

All Articles