codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › C deallocate memory

C deallocate memory

Free what you allocate.

Every malloc needs one free. Set the pointer to NULL afterwards. Forgetting causes a memory leak.

Example code

#include <stdio.h>
#include <stdlib.h>
int main() {
  int *p = malloc(sizeof(int));
  *p = 42;
  printf("%d\n", *p);
  free(p);
  p = NULL;
  return 0;
}

Run this code in the online editor

← C reallocate memoryC memory mistakes →