用一个命令释放所有malloc()创建的指针?

用一个命令释放所有malloc()创建的指针?,c,free,malloc,C,Free,Malloc,是否有一个线性程序可以释放使用mallocs创建的所有指针占用的内存?或者,这只能通过free分别调用每个指针来手动完成吗?malloc本身具有实现定义的行为。因此,它没有必要跟踪它所有的指针,这显然会阻碍这个想法 您需要创建自己的内存管理器来跟踪指针,然后提供一个名为free\u all的函数,或者通过它拥有的指针列表调用free的函数 注意,这听起来有点糟糕。最好对你的内存使用更严格一点/更负责任一点,并且在你完成任务后,把它释放出来;不要让他们到处闲逛 也许在你想把你的想法应用到哪里的背景

是否有一个线性程序可以释放使用
malloc
s创建的所有指针占用的内存?或者,这只能通过
free
分别调用每个指针来手动完成吗?

malloc
本身具有实现定义的行为。因此,它没有必要跟踪它所有的指针,这显然会阻碍这个想法

您需要创建自己的内存管理器来跟踪指针,然后提供一个名为
free\u all
的函数,或者通过它拥有的指针列表调用
free
的函数

注意,这听起来有点糟糕。最好对你的内存使用更严格一点/更负责任一点,并且在你完成任务后,把它释放出来;不要让他们到处闲逛


也许在你想把你的想法应用到哪里的背景知识再多一点,我们可能会找到更简单的解决方案。

你可以通过在malloc周围创建某种“包装器”来做到这一点。 (警告:这只是显示想法的伪代码,根本没有检查)


但这听起来不是一个好主意,我当然不会这么做,至少对于一般用途的分配/解除分配。当不再需要内存时,您最好负责地分配和释放内存。

您可能希望执行一种称为“竞技场分配”的操作,即从一个公共“竞技场”分配某些请求,完成后可以立即释放这些请求

如果您在Windows上,则可以使用HeapCreate创建竞技场,使用HeapAlloc从刚刚创建的堆/竞技场获取内存,使用HeapDestroy一次性释放所有内存


请注意,当程序退出()时,使用malloc()分配的所有内存都将被释放。

是的,除非您编写自己对
malloc()
free()的定义,否则您可以这样做。您可能应该调用
myCustomMalloc()
而不是常规的
malloc()
,并且应该跟踪某个内存位置中的所有指针,当您调用
myCustomFree()
方法时,应该能够清除使用
myCustomMalloc()创建的所有指针。注意:两个自定义方法都将在内部调用
malloc()
free()


通过这种方式你可以实现你的目标。我是一个喜欢java的人,但在我的早期,我经常用C语言工作。我假设您正在尝试实现一个由编译器处理内存的通用解决方案。正如在Java中看到的那样,这会带来性能成本。您不必担心分配和释放内存。但这对绩效有严重影响。这是一个你必须接受的权衡。

你可能需要研究一下。这些数据结构正是为此而构建的

一个常见的实现是在中,它用于Apache web服务器,以及其他项目,如Subversion。

请查看dlmalloc

看看下面的函数

/*
  mspace is an opaque type representing an independent
  region of space that supports mspace_malloc, etc.
*/
typedef void* mspace;

/*
  create_mspace creates and returns a new independent space with the
  given initial capacity, or, if 0, the default granularity size.  It
  returns null if there is no system memory available to create the
  space.  If argument locked is non-zero, the space uses a separate
  lock to control access. The capacity of the space will grow
  dynamically as needed to service mspace_malloc requests.  You can
  control the sizes of incremental increases of this space by
  compiling with a different DEFAULT_GRANULARITY or dynamically
  setting with mallopt(M_GRANULARITY, value).
*/
mspace create_mspace(size_t capacity, int locked);

/*
  destroy_mspace destroys the given space, and attempts to return all
  of its memory back to the system, returning the total number of
  bytes freed. After destruction, the results of access to all memory
  used by the space become undefined.
*/
size_t destroy_mspace(mspace msp);

...

/*
  The following operate identically to their malloc counterparts
  but operate only for the given mspace argument
*/
void* mspace_malloc(mspace msp, size_t bytes);
void mspace_free(mspace msp, void* mem);
void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
void* mspace_realloc(mspace msp, void* mem, size_t newsize);

这就是我所说的-。-''是的,我也可以在我的回答中包括半本书,但我个人喜欢简短且信息丰富的评论,而不是包含两个有用信息的垃圾评论。你有半本书吗?我希望我有耐心写半本书。“不,这就是为什么”比“不”更能说明问题。此外,如果你想对此大发雷霆,至少我的书是可读的o@n00b32:只有在您实际退出的情况下,此处才允许愤怒退出。不要再发脾气了。“实现定义的行为”在语言标准中有一个特定的含义,它不是一个模糊的术语。@n00b32:没有文化障碍,人们只是不喜欢糟糕的语法和拼写。你误解了我所说的可读性。我的意思不是说可以阅读:这不是一本书。它的意思是很容易阅读。对于英语读者来说,好的英语比糟糕的英语更容易阅读。这就是全部;我不是想攻击你,没有人想发动火焰战。你可能在寻找“记忆竞技场”。这很简单:
exit(0)这就是我最初想说的^^+1,用于考虑内存泄漏(和/或懒惰:-)
exit(0)
将起到作用,但当然我不能再执行任何代码了。;-)
/*
  mspace is an opaque type representing an independent
  region of space that supports mspace_malloc, etc.
*/
typedef void* mspace;

/*
  create_mspace creates and returns a new independent space with the
  given initial capacity, or, if 0, the default granularity size.  It
  returns null if there is no system memory available to create the
  space.  If argument locked is non-zero, the space uses a separate
  lock to control access. The capacity of the space will grow
  dynamically as needed to service mspace_malloc requests.  You can
  control the sizes of incremental increases of this space by
  compiling with a different DEFAULT_GRANULARITY or dynamically
  setting with mallopt(M_GRANULARITY, value).
*/
mspace create_mspace(size_t capacity, int locked);

/*
  destroy_mspace destroys the given space, and attempts to return all
  of its memory back to the system, returning the total number of
  bytes freed. After destruction, the results of access to all memory
  used by the space become undefined.
*/
size_t destroy_mspace(mspace msp);

...

/*
  The following operate identically to their malloc counterparts
  but operate only for the given mspace argument
*/
void* mspace_malloc(mspace msp, size_t bytes);
void mspace_free(mspace msp, void* mem);
void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
void* mspace_realloc(mspace msp, void* mem, size_t newsize);