简体   繁体   English

我可以将指向结构的指针的本地实例添加到用 malloc 分配的内存到结构的全局数组和 free() 正确吗

[英]Can I add a local instance of a pointer to a struct with memory allocated with malloc to a global array of structs and free() properly

Problem问题

Valgrind is informing me of a memory leak, however no matter what I cannot seem to figure out how I haven't freed all my allocated memory, leading to the question in my title. Valgrind 通知我内存泄漏,但是无论如何我似乎都无法弄清楚我是如何没有释放所有分配的内存,这导致了我的标题中的问题。 I won't provide my full code so I also won't attach the entire valgrind report, but I will point out where valgrind think the problem is.我不会提供我的完整代码,所以我也不会附上整个 valgrind 报告,但我会指出 valgrind 认为问题出在哪里。

Some code to further explain what is going on一些代码可以进一步解释发生了什么

assume all memory allcations are checked for error, assume while loop teminates eventually假设检查所有内存分配是否有错误,假设 while 循环最终终止

struct s1{
  int total_s2; // assume some code initialise this to 0
  struct s2 **arr_s2;
} struct1;

struct s2{
  char *important_string
};


void add_s2_to_s1(struct s2 *struct2){
  struct1.arr_s2 = realloc(s1.arr_s2, sizeof(struct s2 *) * s1.total_s2);
  struct1.arr_s2[total_s2 - 1] = malloc(sizeof(struct s2));  // !mem lost!
  struct1.arr_s2[total_s2 - 1] = struct2;
}

// in main
while(some_string){
  struct s2 *struct2 = malloc(sizeof(*struct2));
  struct2->important_string = malloc(sizeof(some_string) + 1);
  s1.total_s2++;
  add_s2_to_s1(struct2);

  // some code to change some_string
}

for(int i = 0; i < total_s2; i++){
  free(s1.arr_s2[i]->important_string);
  free(s1.arr_s2[i]);
}
free(s1.arr_s2);

Bit more on my understanding多一点我的理解

Even though struct2 is technically lost each iteration of the while loop, the pointer to the memory of struct2 should be stored in the array in struct1 , and thus should be able to be freed no problem.尽管struct2在 while 循环的每次迭代中在技术上都会丢失,但指向struct2内存的指针应该存储在struct1的数组中,因此应该可以毫无问题地被释放。

Worst part with mem leak issues are my program is doing exactly what I want it to do right now, so it is very tempting to throw my hands in the air and move on.内存泄漏问题最糟糕的部分是我的程序正在做我希望它现在做的事情,所以我很想把手举到空中继续前进。 But I know I should fix it otherwise it might come back and bite me in the ass later.但我知道我应该修好它,否则它可能会回来咬我的屁股。

This line allocates some bytes and stores a pointer to those bytes in the variable struct1.arr_s2[total_s2 - 1] :此行分配一些字节并将指向这些字节的指针存储在变量struct1.arr_s2[total_s2 - 1]中:

struct1.arr_s2[total_s2 - 1] = malloc(sizeof(struct s2));  // !mem lost!

This line stores a pointer to some other bytes in the variable struct1.arr_s2[total_s2 - 1] :这一行在变量struct1.arr_s2[total_s2 - 1]中存储了一个指向其他字节的指针:

struct1.arr_s2[total_s2 - 1] = struct2;

As you know, a variable only has one value at a time.如您所知,一个变量一次只有一个值。 The variable struct1.arr_s2[total_s2 - 1] now contains the same value as the variable struct2 - it doesn't somehow remember the other value you gave it (the address of some newly allocated bytes).变量struct1.arr_s2[total_s2 - 1]现在包含与变量struct2相同的值——它不记得你给它的另一个值(一些新分配字节的地址)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM