I already read these links: link1 and link2.
However, if I execute the following piece of code inside valgrind:
valgrind --tool=memcheck --leak-check=full --num-callers=40 --show-possibly-lost=no
I can see that the memory is not correctly freed.
#include <stdio.h>
#include <stdlib.h>
void printVector(char ** vector, int N);
void allocateVector(char *** vector, int N, int M);
void deallocateVector(char *** vector, int N);
int main(int argc, char * argv[]) {
char ** vector;
int N=6;
int M=200;
allocateVector(&vector,N,M);
printVector(vector,N);
deallocateVector(&vector,N);
}
void allocateVector(char *** vector, int N, int M) {
*vector=(char **) malloc(N*sizeof(char *));
int i;
for(i=0; i<N; i++) {
(*vector)[i]=(char *) malloc(M*sizeof(char));
(*vector)[i]="Empty";
}
}
void deallocateVector(char *** vector, int N) {
int i;
char ** temp=*vector;
for(i=0; i<N; i++) {
if(temp[i]!=NULL) {
free(temp[i]);
}
}
if(temp!=NULL) {
free(temp);
}
*vector=NULL;
}
I cannot find where is the mistake.