×ðÁú¿­Ê±

ʹÓÃValgrindÔÚLinuxϵͳÉϾÙÐÐÄÚ´æ×ß©¼ì²âµÄÉèÖÃÒªÁì

ʹÓÃvalgrindÔÚlinuxϵͳÉϾÙÐÐÄÚ´æ×ß©¼ì²âµÄÉèÖÃÒªÁì

СÐò£º

ÄÚ´æ×ß©ÊÇÈí¼þ¿ª·¢Àú³ÌÖг£¼ûµÄÎÊÌâÖ®Ò»¡£Ëüͨ³£»áµ¼Ö³ÌÐòÔËÐбäÂý£¬ÉõÖÁÍ߽⡣ΪÁËʵʱ·¢Ã÷Ï¢Õù¾öÕâЩÎÊÌ⣬¿ª·¢Ö°Ô±ÐèÒª½èÖú¹¤¾ßÀ´¾ÙÐÐÄÚ´æ×ß©µÄ¼ì²â¡£ÔÚLinuxϵͳÏ£¬Ò»¸öÆÕ±éʹÓõŤ¾ß¾ÍÊÇValgrind¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖúÍʹÓÃValgrind¾ÙÐÐÄÚ´æ×ß©µÄ¼ì²â£¬²¢Í¨¹ý´úÂëʾÀýÀ´Õ¹Ê¾ÏêϸµÄ²Ù×÷Àú³Ì¡£

°ì·¨Ò»£º×°ÖÃValgrind

ÔÚLinuxϵͳÉÏ×°ÖÃValgrindºÜÊǼòÆÓ¡£ÎÒÃÇ¿ÉÒÔͨ¹ý°ü¹ÜÀí¹¤¾ßÈçapt»òyumÖ±½Ó×°ÖÃValgrind¡£ÔÚUbuntuÉÏ£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº

sudo apt-get install valgrind

µÇ¼ºó¸´ÖÆ

°ì·¨¶þ£º±àд´úÂëʾÀý

ΪÁËÑÝʾValgrindµÄʹÓã¬ÎÒÃDZàдһ¸ö¼òÆÓµÄC³ÌÐò¡£³ÌÐòµÄ¹¦Ð§Êǽ¨ÉèÒ»¸öÕûÐÍÊý×飬ÔÚ³ÌÐò¿¢ÊÂǰûÓÐÊͷŸÃÊý×éËùÕ¼ÓõÄÄÚ´æ¡£ÒÔÏÂÊÇ´úÂëʾÀý£º

#include <stdio.h>
#include <stdlib.h>

void create_array(int length) {
    int* array = malloc(length * sizeof(int));
    for (int i = 0; i < length; i++) {
        array[i] = i + 1;
    }
}

int main() {
    create_array(100);
    return 0;
}

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇÔÚcreate_arrayº¯ÊýÖÐʹÓÃmalloc·ÖÅÉÁËÒ»¶ÎÄڴ棬µ«ÔÚ³ÌÐò¿¢ÊÂǰûÓÐÊͷŸÃÄÚ´æ¡£

°ì·¨Èý£ºÊ¹ÓÃValgrind¾ÙÐÐÄÚ´æ×ß©¼ì²â

ÔÚÖÕ¶ËÖÐÔËÐÐValgrindÏÂÁîÀ´¼ì²âÄÚ´æ×ß©¡£ÒÔÏÂÊÇValgrindÏÂÁîµÄ»ù±¾Óï·¨£º

valgrind [Ñ¡Ïî] [´ý¼ì²âµÄ³ÌÐò¼°²ÎÊý]

µÇ¼ºó¸´ÖÆ

ͨ¹ýÒÔÏÂÏÂÁîÔËÐÐValgrind£¬²¢¼ì²â×ðÁú¿­Ê±´úÂëʾÀý£º

valgrind --leak-check=full ./a.out

µÇ¼ºó¸´ÖÆ

ÉÏÊöÏÂÁîÖУ¬”–leak-check=full”ÌåÏÖ¾ÙÐÐÍêÕûµÄÄÚ´æ×ß©¼ì²â£¬”./a.out”ÌåÏÖÔËÐÐÄ¿½ñĿ¼ÏÂÃûΪ”a.out”µÄ¿ÉÖ´ÐÐÎļþ¡£

°ì·¨ËÄ£ºÆÊÎöValgrindµÄÊä³öЧ¹û

Valgrind»áÊä³öÏêϸµÄÄÚ´æ×ß©¼ì²âЧ¹û¡£ÒÔÏÂÊÇValgrind¶Ô×ðÁú¿­Ê±´úÂëʾÀýµÄÊä³öЧ¹û£º

==18708== Memcheck, a memory error detector
==18708== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==18708== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==18708== Command: ./a.out
==18708== 
==18708== 
==18708== HEAP SUMMARY:
==18708==     in use at exit: 400 bytes in 1 blocks
==18708==   total heap usage: 1 allocs, 0 frees, 400 bytes allocated
==18708== 
==18708== 400 bytes in 1 blocks are definitely lost in loss record 1 of 1
==18708==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18708==    by 0x40059D: create_array (main.c:6)
==18708==    by 0x4005A8: main (main.c:11)
==18708== 
==18708== LEAK SUMMARY:
==18708==    definitely lost: 400 bytes in 1 blocks
==18708==    indirectly lost: 0 bytes in 0 blocks
==18708==      possibly lost: 0 bytes in 0 blocks
==18708==    still reachable: 0 bytes in 0 blocks
==18708==         suppressed: 0 bytes in 0 blocks
==18708== 
==18708== For counts of detected and suppressed errors, rerun with: -v
==18708== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

µÇ¼ºó¸´ÖÆ

ValgrindµÄÊä³öЧ¹û°üÀ¨ÁËÒÔÏÂÖ÷ÒªµÄÐÅÏ¢£º

HEAP SUMMARY: ÓÃÓÚ˵Ã÷ÄÚ´æµÄʹÓÃÇéÐΡ£±¾ÀýÖУ¬³ÌÐòÔÚÍ˳öʱÓÐ400×Ö½ÚµÄÄÚ´æÈÔÈ»±»Ê¹Ó㬲¢ÇÒÖ»¾ÙÐйýÒ»´ÎÄÚ´æ·ÖÅÉ¡£

LEAK SUMMARY: ÓÃÓÚ×ܽáÄÚ´æ×ß©µÄÇéÐΡ£±¾ÀýÖУ¬ÓÐ400×Ö½ÚµÄÄÚ±£´æ´úÂëʾÀýµÄ×îºóδ±»ÊÍ·Å¡£

ERROR SUMMARY: ÓÃÓÚչʾ¹ýʧÐÅÏ¢µÄÕªÒª¡£±¾ÀýÖУ¬Valgrind¼ì²âµ½ÁË1¸ö¹ýʧ¡£

½áÂÛ£º

ValgrindÊÇÒ»¸öÇ¿Ê¢µÄ¹¤¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵʱ·¢Ã÷Ï¢Õù¾öÄÚ´æ×ß©µÈÄÚ´æÏà¹ØµÄÎÊÌâ¡£±¾ÎÄÏÈÈÝÁËÔõÑùÔÚLinuxϵͳÉÏ×°Öá¢ÉèÖúÍʹÓÃValgrind¾ÙÐÐÄÚ´æ×ß©¼ì²â£¬²¢Í¨¹ýÒ»¸ö¼òÆӵĴúÂëʾÀý¾ÙÐÐÁË˵Ã÷¡£Ï£ÍûÕâЩÄÚÈÝÄܹ»×ÊÖú¿ª·¢Ö°Ô±¸üºÃµØ¾ÙÐÐÄÚ´æÖÎÀíºÍµ÷ÊÔÊÂÇé¡£

ÒÔÉϾÍÊÇʹÓÃValgrindÔÚLinuxϵͳÉϾÙÐÐÄÚ´æ×ß©¼ì²âµÄÉèÖÃÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿