GCC中是否有任何编译器选项可以在编译时检查内存错误?

San*_*raj 1 c gcc

我正在编译这个程序,编译顺利.我执行它的那一刻,它失败了,free(): invalid pointer错误.

#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;
}
Run Code Online (Sandbox Code Playgroud)

我使用gcc -o memtest m.c命令编译.

是否有任何GCC编译器选项会在编译期间给出关于这些无效指针错误的警告/错误/指示?

Ign*_*ams 5

不,请使用电围栏Valgrind.