I was compiling this program and the compilation went fine. The moment I executed it, it failed with free(): invalid pointer
error.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p = NULL;
if ((p = (char *) malloc((int)sizeof(char) * 100)) == NULL) {
printf("ERROR: unable to allocate memory\n");
return -1;
}
p += 50;
free(p);
return 0;
}
I compiled using gcc -o memtest m.c
command.
Are there any GCC
compiler options that will give a warning/error/indication about these invalid pointer errors during compile time?