什么是C中的内存泄漏?

问题描述 投票:2回答:2

我试图解决exercise 16的额外信用。即使编译正确,我也会发生内存泄漏。

现在,我的想法是,如果根本不使用malloc(),该程序将不会泄漏内存,但是在这里它确实会泄漏,因为当我运行命令时:

valgrind --leak-check=full -v ./ex16-1

我知道了:

definitely lost: 21 bytes in 2 blocks
  • 即使我不分配任何内存(如果我的源没有问题,也有可能泄漏)吗?
  • 此外,如果可能的话,我该如何释放该内存块?内存指向哪里?

full output of valgrind is available on Pastebin

source code

#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;
  who.name = strdup(name);
  who.age = age;
  who.height = height;
  who.weight = weight;

  return 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 const *argv[]) {
  struct Person joe = Person_create("Joe Alex", 32, 64, 140);
  struct Person frank = Person_create("Frank Blank", 20, 72, 180);

  Person_print(joe);
  Person_print(frank);
  return 0;
}
c memory-leaks
2个回答
3
投票

此程序表明内存泄漏严重。在许多系统上,它不会运行太远。

#include <memory.h>

int add (int a, int b)
{
      char *hog = malloc (1024 * 1024 * 1024 * 50);
      return a + b;
}
int main (void)
{
    int sum = add (add (6, 8), add (3, 7));
    return 0;
}

对于您的程序,它调用strdup(),后者调用malloc。当您完成strdup的返回值时,应将其释放。


2
投票

当您动态分配内存时,您有责任释放已分配的内存,即将其交还给操作系统,以便在无法释放内存并且事实证明足够的内存时可以重用它,您的系统可能会用完内存,导致所有正在运行的程序失败,并且新程序将无法启动。

如果您不使用malloc(),但是您使用的是某些库或标准库函数,则将发生泄漏,一种方法是

void function()
 {
    FILE *file;
    file = fopen("/some/path/file.txt", "r");
    /* check that file is valid and use it */
 }

上面的函数会泄漏内存,因为fopen()分配的某些资源未释放,因此需要fclose(file)来防止泄漏。

使用valgrind,您可能会发现没有明显调用malloc()或代码中任何相关函数的情况,但它会向您报告已分配的内存和可能已释放的内存。

© www.soinside.com 2019 - 2024. All rights reserved.