how to free memory in c
snippet in c

free in c

user1420

the C library function void free(void *ptr) 
  deallocates the memory previously allocated by a call to calloc, malloc, or realloc.

how to dynamically allocate array size in c

user9842


// declare a pointer variable to point to allocated heap space
int    *p_array;
double *d_array;

// call malloc to allocate that appropriate number of bytes for the array

p_array = (int *)malloc(sizeof(int)*50);      // allocate 50 ints
d_array = (int *)malloc(sizeof(double)*100);  // allocate 100 doubles


// use [] notation to access array buckets 
// (THIS IS THE PREFERED WAY TO DO IT)
for(i=0; i < 50; i++) {
  p_array[i] = 0;
}

// you can use pointer arithmetic (but in general don't)
double *dptr = d_array;    // the value of d_array is equivalent to &(d_array[0])
for(i=0; i < 50; i++) {
  *dptr = 0;
  dptr++;
}

malloc in c

user2737

ptr = (cast-type*) malloc(byte-size)

how to free memory in c

user5146

int *example = NULL; //create pointer
example = malloc(sizeof(int)); //allocate memory
free(example); //deallocate memory