valgrind如何计算我的示例内存泄漏?

时间:2014-12-21 19:56:06

标签: c memory valgrind

我有目的地删除了结构的释放,并想了解valgrind如何计算内存泄漏。

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

struct Person{
    char *name;
    int age;
    int height;
    int weight;
};

struct Person *Person_create(char *name, int age, int height, int weight){
    struct Person *who = malloc(sizeof(struct Person));
    assert(who != NULL);

    who->name = strdup(name);
    who->age = age;
    who->height = height;
    who->weight = weight;

    return who;
}

void Person_destroy(struct Person *who){
    assert(who != NULL);
    free(who->name);
    free(who);
}

void Person_print(struct Person *who){
    printf("Name: %s\n", who->name);
    printf("\tAge: %d\n", who->age);
    printf("\tHeight: %d\n", who->height);
    printf("\tWeight: %d\n", who->weight);
}

int main(int argc, char *argv[]){
    //make two people structures
    struct Person *joe = Person_create("Joe Alex", 32, 64, 140);
    struct Person *frank = Person_create("Frank Blank", 20, 72, 180);

    //print them out and where they are in memory
    printf("Joe is at memory location %p\n", joe);
    Person_print(joe);

    printf("Frank is at memory location %p\n", frank);
    Person_print(frank);

    //make everyone age 20 years and print them again
    joe->age += 20;
    joe->height -= 2;
    joe->weight += 40;
    Person_print(joe);

    frank->age += 20;
    frank->weight += 20;
    Person_print(frank);

//  Person_destroy(joe);
//  Person_destroy(frank);

    return 0;
}

Valgrind输出:

==7687== HEAP SUMMARY:
==7687==     in use at exit: 69 bytes in 4 blocks
==7687==   total heap usage: 4 allocs, 0 frees, 69 bytes allocated
==7687== 
==7687== 33 (24 direct, 9 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 4
==7687==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7687==    by 0x40067B: Person_create (ex16.c:14)
==7687==    by 0x4007B9: main (ex16.c:40)
==7687== 
==7687== 36 (24 direct, 12 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==7687==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7687==    by 0x40067B: Person_create (ex16.c:14)
==7687==    by 0x4007D6: main (ex16.c:41)
==7687== 
==7687== LEAK SUMMARY:
==7687==    definitely lost: 48 bytes in 2 blocks
==7687==    indirectly lost: 21 bytes in 2 blocks
==7687==      possibly lost: 0 bytes in 0 blocks
==7687==    still reachable: 0 bytes in 0 blocks
==7687==         suppressed: 0 bytes in 0 blocks
==7687== 
==7687== For counts of detected and suppressed errors, rerun with: -v
==7687== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

如果我没有释放我创建的两个结构,泄漏应该是4 * 4 * 2 = 32字节。为什么总字节数会泄漏48?

来自uname -a的平台信息: Linux mymachine 3.13.0-43-generic#72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014 x86_64 x86_64 x86_64 GNU / Linux

2 个答案:

答案 0 :(得分:4)

您正在为具有32位整数的64位平台进行编译,并且结构布局如下:

char *name;  /* 8 bytes */
int age;     /* 4 bytes */
int height;  /* 4 bytes */
int weight;  /* 4 bytes */
             /* 4 bytes of padding to make it possible to create */
             /*   correctly-aligned arrays of struct Person      */

因此,sizeof(struct Person)24,而不是16。其中两个产生48个字节。

有关填充和对齐的讨论,请参阅Why isn't sizeof for a struct equal to the sum of sizeof of each member?

答案 1 :(得分:0)

当你要求M个字节时,库会分配M + x个字节,其中&#39; x&#39;是需要跟踪的数字。那些可能是标题,可能会将存储量四舍五入到某个标准块。