1. -g 编译选项,加入符号表,在crash之后,能找到对应行;
2. 内置函数:
__sync_add_and_fetch 等函数,gcc自带函数,可以实现原子操作
__builtin_return_address 获取当前frame指针,可用于获取函数调用栈
3. --wrap功能 gcc -o test main.c -Wl,--wrap,malloc -Wl,--wrap,free (定制化malloc和free函数)
void * __wrap_malloc(int size)
{
malloc_count++;
return __real_malloc(size);
}
void __wrap_free(void *ptr)
{
free_count++;
__real_free(ptr);
}
int main()
{
malloc_count = 0;
free_count = 0;
int *p1 = (int *)malloc(sizeof(int));
int *p2 = (int *)malloc(sizeof(int));
free( p1);
if(malloc_count != free_count)
printf("memory leak!\n");
return 0;
}
4.编译宏
__COUNTER__ : 每出现一次就累加一次
5. -fsanitize=address
gcc编译时加上-fsanitize=address,编译好的二进制文件,运行结束打印内存统计信息,类似valgrind
gcc -fsanitize=address -g -o memDebug memDebug.c
6.其他待续.....
2306

被折叠的 条评论
为什么被折叠?



