简体   繁体   English

如何一次释放所有已分配的内存?

[英]How can I free all allocated memory at once?

Here is what I am working 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 memory allocated is released one at a time using free(). 使用数据后,使用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 a way to release all the allocated memory at once, for this array? 有没有办法一次性释放所有已分配的内存,对于这个阵列?

Thank You. 谢谢。

Not with the standard malloc() allocator - you need to investigate the use of memory pools. 不使用标准的malloc()分配器 - 您需要调查内存池的使用。 These work by allocating a big block of memory up-front, allocating from it and freeing back to it as you request via their own allocation functions, and then freeing the whole lot with a special "deallocate all" function. 这些工作通过预先分配大块内存,从中分配并在您通过自己的分配函数请求时释放回来,然后使用特殊的“deallocate all”函数释放整个批次。

I must say I've always found these things a bit ugly - it really isn't that hard to write code that doesn't leak. 我必须说我总是发现这些东西有点难看 - 编写不泄漏的代码真的不难。 The only reason I can see for using them is to mitigate heap fragmentation, if that is a real problem for you. 我可以看到使用它们的唯一原因是缓解堆碎片,如果这对您来说是一个真正的问题。

No there is not. 不,那里没有。 Memory which is separately allocated must be separately freed. 必须单独释放单独分配的内存。

The only way you could free it as once is if you allocated it at once is a giant block. 你可以一次性释放它的唯一方法是,如果你立刻分配它是一个巨大的块。 You would then have to do a bit of pointer math to assign every row the correct index into the array but it's not terribly difficult. 然后你必须做一些指针数学运算,为每一行分配正确的索引到数组中,但这并不是非常困难。 This approach does have a few downsides though 这种方法确实有一些缺点

  1. Extra pointer math 额外的指针数学
  2. Requires one giant contiguous block of memory vs. N smaller blocks of memory. 需要一个巨大的连续内存块而不是N个较小的内存块。 Can be an issue in low memory or high fragmentation environments. 在低内存或高碎片环境中可能是一个问题。
  3. Extra work for no real stated gain. 额外的工作没有实际的收益。

If you want to release it all at once, you have to allocate it all at once. 如果要立即释放所有内容,则必须立即分配所有内容。

A simple manual solution, if you know the total size you'll need in advance, is to allocate it all in once chunk and index into it as appropriate. 一个简单的手动解决方案,如果你事先知道你需要的总大小,就是将它全部分配到一个块中并在适当时将其索引到它中。 If you don't know the size in advance you can use realloc to grow the memory, so long as you only access it indexed from the initial pointer, and don't store additional pointers anywhere. 如果您事先不知道大小,可以使用realloc来增加内存,只要您只从初始指针索引它,并且不在任何地方存储其他指针。

That being said, direct allocation and deallocation is a simple solution, and harder to get wrong than the alternatives. 话虽如此,直接分配和解除分配是一个简单的解决方案,并且比其他选择更难以出错。 Unless the loop to deallocate is causing you real difficulties, I would stick with what you have. 除非解除分配的循环导致你真正的困难,否则我会坚持你拥有的东西。

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

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