Point 1
You cannot free some memory. You have to free all. To elaborate, whatever memory has been allocated by a single call to malloc() or family, will be free-d at a time. You cannot free half (or so) of the allocated memory.
Point 2
- Please do not cast the return value of
malloc() and family in C.
- You should always write
sizeof(*ptr) instead of sizeof(type*).
Point 3
You can use the free() to free the allocated memory.
for example, see the below code and please notice the inline comments.
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main(void) //notice the signature of main
{
int **p = NULL; //always initialize local variables
int i = 0, j = 0;
p = malloc(MAXROW * sizeof(*p)); // do not cast and use sizeof(*p)
if (p) //continue only if malloc is a success
{
//do something
//do something more
free(p); //-----------> freeing the memory here.
}
return 0;
}