04-常见内存错误以及valgrind使用
04-常见内存错误以及valgrind使用
代码段:
仅仅读数据,因此对这一部分的数据。试图写仅仅读数据,这个在编译的时候基本上能够检測。
数据段/BSS段:
未初始化直接訪问,即使没有显示初始化,仍然会初始化为0
栈空间数据:
(1)局部变量。未初始化变量会给随机的初值。出现异常情况更诡异
(2)栈溢出:在栈中申请过大的局部变量
堆空间数据
内存泄露 (1)申请未释放(2)申请后。双重释放
对于全部的地址空间:
(1) 野指针的问题:未初始化指针。会訪问这个指针指向的空间
(2) 越界訪问:比如一个数据a[10] ,试图訪问啊a【10】以及以后数据
(3) 非法的越权訪问 比如:mmap的空间仅仅读,但试图写
(4) 空间不在控制范围仍然去訪问空间。比如返回局部变量地址,且后面訪问这个空间
尽管在编程时尽量避免这些问题,可是一旦出现故障。要善于使用工具去检測我们的内存错误
能够使用工具
$Sudo apt-get install valgrind
$ valgrind --tool=memcheck--show-reachable=yes --read-var-info=yes --verbose --time-stamp=yes --leak-check=full--log-file=mycode.log ./valgrind_example01
假设要使用图形化的工具,须要安装QT 这个工具名字叫 Valkyrie
第一、自己在编程中,要避免一些常见的错误。第二、要善于使用工具去检測我们的内存错误。
測试样例:
root@ubuntu:~/wangyiStudy# ls
valgrind_example01.c
root@ubuntu:~/wangyiStudy#gcc -g -o valgrind_example01 valgrind_example01.c
root@ubuntu:~/wangyiStudy# ls
valgrind_example01 valgrind_example01.c
root@ubuntu:~/wangyiStudy#valgrind --tool=memcheck --show-reachable=yes --read-var-info=yes --verbose--time-stamp=yes --leak-check=full --log-file=mycode.log ./valgrind_example01
root@ubuntu:~/wangyiStudy# ls
mycode.log valgrind_example01 valgrind_example01.c
測试程序代码:valgrind_example01.c
root@ubuntu: # morevalgrind_example01.c
#include<stdio.h>
#include<stdlib.h>
int
main(void){
int i[5];
if(i[0] == 0)
i[1] = 1;
char *ptr1;
*ptr1 = 'c';
char *ptr = malloc(100);
free(ptr);
free(ptr);
return 0;
}
查看生成的日志:
root@ubuntu:~/wangyiStudy# moremycode.log
==00:00:00:03.1165282== HEAP SUMMARY:
==00:00:00:03.1165282== in use at exit: 0 bytes in 0blocks
==00:00:00:03.1165282== totalheap usage: 1 allocs, 2 frees, 100 bytes allocated //一次申请。双重释放
==00:00:00:03.1165282==
==00:00:00:03.1165282== All heap blocks were freed -- no leaks are possible
==00:00:00:03.1165282==
==00:00:00:03.1165282== Use --track-origins=yes to see where uninitialised values come from
==00:00:00:03.1165282== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
==00:00:00:03.1165282==
==00:00:00:03.1165282== 1 errors in context 1 of 3:
==00:00:00:03.1165282== Invalid free() / delete / delete[] / realloc()
==00:00:00:03.1165282== at 0x4C2BDEC: free (in/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==00:00:00:03.1165282== by 0x4005BF: main(valgrind_example01.c:15)
==00:00:00:03.1165282== Address 0x51fd040 is 0 bytes insidea block of size 100 free'd
==00:00:00:03.1165282== at 0x4C2BDEC: free (in/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==00:00:00:03.1165282== by 0x4005B3: main(valgrind_example01.c:14)
==00:00:00:03.1165282==
==00:00:00:03.1165282==
==00:00:00:03.1165282== 1 errors in context 2 of 3:
==00:00:00:03.1165282== Use of uninitialised value of size 8
==00:00:00:03.1165282== at 0x400597: main(valgrind_example01.c:11)
==00:00:00:03.1165282==
==00:00:00:03.1165282==
==00:00:00:03.1165282== 1 errors in context 3 of 3:
==00:00:00:03.1165282== Conditional jump or move depends on uninitialised value(s) //未初始化
==00:00:00:03.1165282== at 0x40058A: main (valgrind_example01.c:8) // 错误定位
==00:00:00:03.1165282==
==00:00:00:03.1165282== ERROR SUMMARY: 3 errors from 3 contexts(suppressed: 0 from 0) // 总计 3 出错误