gpt4 book ai didi

c - gcc 编译器是否有任何选项可以在编译时识别内存损坏?

转载 作者:太空狗 更新时间:2023-10-29 17:20:40 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <string.h>

int main()
{
char arrDst[5] = {0};
char arrSrc[10] = "123456";
memcpy( arrDst, arrSrc, sizeof( arrSrc ) );
return 0;
}

在此程序中,很明显存在内存损坏。

gcc 编译器中是否有任何选项可以让我在编译时识别这个问题?

注意:我使用了 valgrind --leak-check=full,但没有帮助。

最佳答案

$ gcc -Wall -O1 t.c 
In file included from /usr/include/string.h:642:0,
from t.c:3:
In function ‘memcpy’,
inlined from ‘main’ at t.c:13:9:
/usr/include/bits/string3.h:52:3: warning: call to __builtin___memcpy_chk
will always overflow destination buffer [enabled by default]

GCC 可以识别其中的一些。这通常需要打开优化(至少 -01)和警告(-Wall,还要添加 -Wextra)。

关于c - gcc 编译器是否有任何选项可以在编译时识别内存损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16356369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com