是否有任何GCC编译器选项在编译时检查内存错误? [英] Is there any compiler option in GCC to check for memory errors at compile time?

查看:118
本文介绍了是否有任何GCC编译器选项在编译时检查内存错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写这个程序,编译了罚款。无效的指针错误:我执行它的那一刻,它与免费()失败。

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;
}

我编译使用的gcc -o MEMTEST M.C 命令。

是否有将在编译时提供有关这些无效指针错误的警告/错误/指示任何 GCC 编译器选项?

Are there any GCC compiler options that will give a warning/error/indication about these invalid pointer errors during compile time?

推荐答案

没有。使用电围栏 Valgrind的

这篇关于是否有任何GCC编译器选项在编译时检查内存错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆