简体   繁体   中英

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

I am starting to learn dynamic memory allocation. In this code, I have created in main function 2d array:

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).

Then I change the board with other functions. Valgrind returs:

==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:

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. I would appreciate your hints and explanations. `

When you call 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 .

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 .

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. In most cases, each call to malloc(...) should have an equal and opposite call to free(...)

This is important because your system has a finite amount of 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".

So for you, calling free(...) correctly, is going to look something like this:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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