简体   繁体   English

在哪里可以释放 memory,同时创建二维数组? valgrind 错误

[英]Where to free memory, while creating 2d array? valgrind error

I am starting to learn dynamic memory allocation.我开始学习动态 memory 分配。 In this code, I have created in main function 2d array:在这段代码中,我在function 二维数组中创建了:

int r, c;
scanf("%d %d\n", &r, &c);
size_t row = (size_t) r;
size_t col = (size_t) c;
int **board = malloc(row * sizeof(*board));                                                                       
for (int i = 0; i < r; i++) {                                                                                                     
   board[i] = malloc(col * sizeof(*board[i]));
}

(I need both int and size_t because of for loops, especially when r>=0, which is always true for size_t). (由于 for 循环,我需要 int 和 size_t,尤其是当 r>=0 时,这对于 size_t 总是正确的)。

Then I change the board with other functions.然后我用其他功能更换电路板。 Valgrind returs: Valgrind 回归:

==59075== 364 (56 direct, 308 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2                    
==59075==    at 0x483577F: malloc (vg_replace_malloc.c:299)                                                             
==59075==    by 0x10B4DB: main (name22.c:122)                                                                        
==59075==  

122 line is: 122行是:

int **board = malloc(row*sizeof( *board));

I suppose I have to use free(), but after checking other answers I don't know, where is my mistake and where to put free(), if use use this table through the rest of the program.我想我必须使用free(),但是在检查了我不知道的其他答案之后,我的错误在哪里以及将free()放在哪里,如果使用通过程序的rest使用这个表。 I would appreciate your hints and explanations.我会很感激你的提示和解释。 ` `

When you call int **board = malloc(row * sizeof(*board));当你调用int **board = malloc(row * sizeof(*board)); , the system will allocate you row * sizeof(*board) bytes of memory and then return a pointer to the start of that memory (ie - a memory address), which you are storing in the int ** variable called board . ,系统将为您分配 memory 的row * sizeof(*board)字节,然后返回指向该 memory 开头的指针(即 - 一个名为 memory 变量的board ,您存储在int **地址中)

When your program finishes, the system will reclaim that memory, so if your program is short lived, it probably doesn't matter very much, but it's good practise to get into the habit of freeing your memory because it will not reclaim any memory at all until your program exits, unless you tell it to .当你的程序完成时,系统会回收 memory,所以如果你的程序是短暂的,它可能并不重要,但是养成释放你的 memory 的习惯是个好习惯,因为它不会回收任何 ZE299B4935619806CD直到的程序退出,除非你告诉它

For that reason, you should always call free(...) , passing in the memory address you were given when you first called malloc(...) , once you are done with that memory.出于这个原因,一旦你完成了 memory,你应该总是调用free(...) ,传入你第一次调用malloc(...)时给出的 memory 地址。 In most cases, each call to malloc(...) should have an equal and opposite call to free(...)大多数情况下,对malloc(...)的每次调用都应该对free(...)进行相等且相反的调用

This is important because your system has a finite amount of memory.这很重要,因为您的系统具有有限数量的 memory。 If your program is asking for resources and then never giving them back when they are done you will eventually run out of memory - this is what is called a "memory leak".如果您的程序要求资源,然后在完成后从不归还它们,您最终将用完 memory - 这就是所谓的“内存泄漏”。

So for you, calling free(...) correctly, is going to look something like this:所以对你来说,正确调用free(...)看起来像这样:

int **board = malloc(row * sizeof(*board));                                                                       
for (int i = 0; i < r; i++) {                                                                                                     
   board[i] = malloc(col * sizeof(*board[i]));
}

// Do something with board

for (int i = 0; i < r; i++) {
   free(board[i]);
}
free(board);

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

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