??????????????????????
???????д C ?????????????????????????????????????????????????????????????????????????????????????????????????????????鱻???????????????????????????????????й?????汻???????????
??????Щ????????????????????????????????????????????β???????????????????ɡ????????д???????????????????????????????????????????ɡ??????????????????????????Щ???????????????
????C ???????????????????????????????з????????????·?????????????????????????????????????????????? 7 ???????з?????????????????? 3 ???????н????????????????????????????????????????????????????????????????????? 0.001??????????????????????????????????÷?????Ω??????????????????????????????????????????????裬????????????????·??????????????????·??????Ч??????
?????????????????????д???????????????????????????????????????? C++ ?????У????????????????????μ?????——this ??????????????????з??????????????????????????????????????? C++ ???????????????????????????????????????????????????????????????????????????????????????????????? this ?????????????????????????????????????? delete this???????????????????????????????κ???????????? C++ ???????????????????????????????????????? delete ???????????????????????????????μ????й???????С?
??????????????????У????????????????????????????磬??????? A ????????? B???????? B ??????????? A?????????????????????????????????????????????????????????????????????????????????????????????????????????????? A ?????? B??B ?????? C?? C ?????? D?? D ?????? B ?? E??E ?????? A……??????????????????????????ü???????????????????????????????????????????????????????????????????????й??????????????????????????????????????????????????????????????????????
???????????????У????й??????????????????????????麯???У?????????????????????????麯??ū??β??????????????????????????????????????????й???
?????????????κ????????????????????????????й?????——????? Rust ???????????????????????????????????????????????????????????????????????????????????Σ???????????н???????????????????????????????????????????????????????????????????????????????? C ???????????——???????ζ?????????????????????????????????????????????? C ???????????檔
?????? Valgrind ???????? C ???????????
????Linux ?????????????????? C ??????????????——valgrind?????????????????????????????valgrind ???????????д?????????——???????????δ?????????棬?????????????棬????????????渲????????й??????
??????????????????The Valgrind Quick Start Guide????С?????
????#include
????void f(void)
????{
????int* x = malloc(10 * sizeof(int));
????x[10] = 0;
????}
????int main(void)
????{
????f();
????return 0;
????}
?????????????? f ?????м??????????й?????????????????????????????????? valgrind-demo.c ????У??????? gcc ????????
????$ gcc -g -O0 valgrind-demo.c -o valgrind-demo
????????? valgrind ?????????????????????????????????????????????? -g??????????????????? -O0??
????????? valgrind ??? valgrind-demo ????
????$ valgrind --leak-check=yes ./valgrind-demo
??????? valgrind ????????????
==10000== Memcheck?? a memory error detector
==10000== Copyright (C) 2002-2015?? and GNU GPL'd?? by Julian Seward et al.
==10000== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==10000== Command: ./valgrind-demo
==10000==
==10000== Invalid write of size 4
==10000==    at 0x400574: f (valgrind-demo.c:6)
==10000==    by 0x400585: main (valgrind-demo.c:11)
==10000==  Address 0x51d3068 is 0 bytes after a block of size 40 alloc'd
==10000==    at 0x4C29FE0: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==10000==    by 0x400567: f (valgrind-demo.c:5)
==10000==    by 0x400585: main (valgrind-demo.c:11)
==10000==
==10000==
==10000== HEAP SUMMARY:
==10000==     in use at exit: 40 bytes in 1 blocks
==10000==   total heap usage: 1 allocs?? 0 frees?? 40 bytes allocated
==10000==
==10000== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==10000==    at 0x4C29FE0: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==10000==    by 0x400567: f (valgrind-demo.c:5)
==10000==    by 0x400585: main (valgrind-demo.c:11)
==10000==
==10000== LEAK SUMMARY:
==10000==    definitely lost: 40 bytes in 1 blocks
==10000==    indirectly lost: 0 bytes in 0 blocks
==10000==      possibly lost: 0 bytes in 0 blocks
==10000==    still reachable: 0 bytes in 0 blocks
==10000==         suppressed: 0 bytes in 0 blocks
==10000==
==10000== For counts of detected and suppressed errors?? rerun with: -v
==10000== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
????valgrind ????????? valgrind-demo ?????д??????????????????????
????==10000== Invalid write of size 4
????==10000==    at 0x400574: f (valgrind-demo.c:6)
????==10000==    by 0x400585: main (valgrind-demo.c:11)
????==10000==  Address 0x51d3068 is 0 bytes after a block of size 40 alloc'd
????==10000==    at 0x4C29FE0: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
????==10000==    by 0x400567: f (valgrind-demo.c:5)
????==10000==    by 0x400585: main (valgrind-demo.c:11)
??????? valgrind ??????? valgrind-demo ?????д??? 40 ???????й???????
????==10000== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
????==10000==    at 0x4C29FE0: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
????==10000==    by 0x400567: f (valgrind-demo.c:5)
????==10000==    by 0x400585: main (valgrind-demo.c:11)
?????????????????????????????????? valgrind ??????????????????????????????????С?
????????????????????????????valgrind ????к??????ù?????????????????顢???????????????????????? valgrind ?ж??????????????????????????????????????????????????? valgrind ??????????????????????