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.
谢谢你!
3 个解决方案
#1
13
Not with the standard malloc() allocator - you need to investigate the use of memory pools. 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.
不使用标准的malloc()分配器——您需要研究内存池的使用。它们的工作方式是预先分配一大块内存,从中进行分配,并根据您的请求通过它们自己的分配函数释放回内存,然后使用一个特殊的“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.
我必须说,我总是觉得这些东西有点难看——编写不泄漏的代码并不难。我能看到使用它们的唯一原因是减少堆碎片,如果这对您来说是一个真正的问题。
#2
5
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
你唯一能将它释放出来的方法是,如果你立即分配它,它就是一个巨大的块。然后,你需要做一些指针运算,把正确的索引分配给数组中的每一行,但这并不难。不过,这种方法也有一些缺点
- Extra pointer math
- 额外的指针数学
- Requires one giant contiguous block of memory vs. N smaller blocks of memory. Can be an issue in low memory or high fragmentation environments.
- 需要一个巨大的连续内存块和N个较小的内存块。可能是低内存或高碎片化环境中的问题。
- Extra work for no real stated gain.
- 额外的工作没有真正规定的收益。
#3
5
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.
也就是说,直接分配和重新分配是一个简单的解决方案,而且比其他方案更难出错。除非这个循环能给你带来真正的困难,否则我会坚持你所拥有的。
#1
13
Not with the standard malloc() allocator - you need to investigate the use of memory pools. 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.
不使用标准的malloc()分配器——您需要研究内存池的使用。它们的工作方式是预先分配一大块内存,从中进行分配,并根据您的请求通过它们自己的分配函数释放回内存,然后使用一个特殊的“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.
我必须说,我总是觉得这些东西有点难看——编写不泄漏的代码并不难。我能看到使用它们的唯一原因是减少堆碎片,如果这对您来说是一个真正的问题。
#2
5
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
你唯一能将它释放出来的方法是,如果你立即分配它,它就是一个巨大的块。然后,你需要做一些指针运算,把正确的索引分配给数组中的每一行,但这并不难。不过,这种方法也有一些缺点
- Extra pointer math
- 额外的指针数学
- Requires one giant contiguous block of memory vs. N smaller blocks of memory. Can be an issue in low memory or high fragmentation environments.
- 需要一个巨大的连续内存块和N个较小的内存块。可能是低内存或高碎片化环境中的问题。
- Extra work for no real stated gain.
- 额外的工作没有真正规定的收益。
#3
5
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.
也就是说,直接分配和重新分配是一个简单的解决方案,而且比其他方案更难出错。除非这个循环能给你带来真正的困难,否则我会坚持你所拥有的。