I was wondering if it's necessary to free the allocated memory in the following example:
Code:
#include <stdio.h>
int main()
{
char **data = malloc(sizeof(char *) * 5);
if(data == NULL) {
return 0;
}
int i;
for(i = 0; i < 5; i++) {
data[i] = malloc(sizeof(char) * 10);
if(data[i] == NULL) {
// Should I free memory here?
return 0;
}
}
return 0;
}
Let's say the first two times memory gets allocated, so data[0] & data[1] are not NULL. Now if the third time malloc() returns NULL, should I first free the memory in data[0] & data[1] before terminating the program or is this unnecessary since the program quits immediatly after?