Advertisement
RicardasSim

free, make pointer NULL

May 25th, 2020
1,776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. //-std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main()
  7. {
  8.  
  9.     char *ptr = NULL;
  10.  
  11.     printf("(1) %p\n", (void*) ptr);
  12.  
  13.     ptr = malloc(10);
  14.  
  15.     if (!ptr)
  16.     {
  17.         printf("error: cannot allocate memory.\n");
  18.  
  19.         return 1;
  20.     }
  21.  
  22.     printf("(2) %p\n", (void*) ptr);
  23.  
  24.     free(ptr);
  25.  
  26.     printf("(3) %p\n", (void*) ptr);
  27.  
  28.     ptr = NULL;
  29.  
  30.     printf("(4) %p\n", (void*) ptr);
  31.  
  32.     return 0;
  33. }
  34.  
  35. /*
  36. output:
  37.  
  38. (1) (nil)
  39. (2) 0x557cbfb32670
  40. (3) 0x557cbfb32670
  41. (4) (nil)
  42.  
  43. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement