做微信小程序是不是不用做网站,做网站seo优化,网页制作软件有哪些,关于校园推广的软文AddressSanitizer 是一种内存错误检测工具#xff0c;编译时添加 -fsanitizeaddress 选项可以在运行时检测出非法内存访问#xff0c;当发生段错误时#xff0c;AddressSanitizer 会输出详细的错误报告#xff0c;包括出错位置的代码行号和调用栈#xff0c;有助于快速定位…AddressSanitizer 是一种内存错误检测工具编译时添加 -fsanitizeaddress 选项可以在运行时检测出非法内存访问当发生段错误时AddressSanitizer 会输出详细的错误报告包括出错位置的代码行号和调用栈有助于快速定位问题。
demo.c段错误程序
#include stdio.h
#include assert.hvoid buggy_function(int *ptr) {// 确保指针不是 NULL//assert(ptr ! NULL);*ptr 10;
}int main() {int *ptr NULL;*ptr 10;return 0;
}
gcc编译
gcc -g -fsanitizeaddress demo.c
可以在运行程序时通过设置 ASAN_OPTIONS 环境变量将 AddressSanitizer 的输出重定向到文件。具体命令如下
export ASAN_OPTIONSlog_pathasan_log.txt
./a.out
生成日志如下
bwtonDESKTOP-UJNM808:~/project/tmp$ cat asan_log.txt.302302ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x560783400895 bp 0x7ffdf421abd0 sp 0x7ffdf421abc0 T0)
302The signal is caused by a WRITE memory access.
302Hint: address points to the zero page.#0 0x560783400894 in main /home/bwton/project/tmp/demo.c:12#1 0x7ff6d0eb4c86 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.60x21c86)#2 0x560783400719 in _start (/home/bwton/project/tmp/a.out0x719)AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/bwton/project/tmp/demo.c:12 in main
302ABORTING
bwtonDESKTOP-UJNM808:~/project/tmp$
Eclipse编译
如果是嵌入式设备使用Eclipse编译可如下设置:
1. 添加编译选项 在编译器的设置中找到 Miscellaneous杂项部分。 在 Other flags其他标志框中添加 -fsanitizeaddress。 如果你使用的是 C 编译器也可以在 GCC C Compiler 下重复相同的步骤。
2. 修改链接器设置
为了确保 AddressSanitizer 正常工作还需要在链接器中添加相应的选项 在同样的 Tool Settings 下选择 GCC C Linker。 在 Miscellaneous 部分的 Other flags 中添加 -fsanitizeaddress。 如果运行时报错error while loading shared libraries: libasan.so.1:可以在编译时添加静态链接-static-libasan