How can I free all allocated memory at once?

Here is what I work with:

char* qdat[][NUMTBLCOLS];
char** tdat[];
char* ptr_web_data;

    // Loop thru each table row of the query result set
    for(row_index = 0; row_index < number_rows; row_index++)
    {
        // Loop thru each column of the query result set and extract the data
        for(col_index = 0; col_index < number_cols; col_index++)
        {
            ptr_web_data = (char*) malloc((strlen(Data) + 1) * sizeof(char));
            memcpy (ptr_web_data, column_text, strlen(column_text) + 1);
            qdat[row_index][web_data_index] = ptr_web_data;
        }
     }

    tdat[row_index] = qdat[col_index];

After the data is used, the allocated memory is allocated one at a time using free ().

for(row_index = 0; row_index < number_rows; row_index++)
{ 
  // Loop thru all columns used
  for(col_index = 0; col_index < SARWEBTBLCOLS; col_index++)
  {
    // Free memory block pointed to by results set array
    free(tdat[row_index][col_index]);
  }

}

Is there any way to free all allocated memory at once, for this array?

Thank.

+3
source share
3 answers

Not with the standard malloc () allocator - you need to investigate the use of memory pools. They work by allocating a large block of memory up, allocating it and freeing it back when you request through your own distribution functions, and then freeing the entire batch with the special function “free all”.

, - , . , , - , .

+13

, . , , .

- , . , , . ,

  • N . .
  • .
+5

, .

, , , , . , realloc , .

, , . , , .

+5

Source: https://habr.com/ru/post/1742882/


All Articles