如何在C语言中执行自动内存管理?

如何在C语言中执行自动内存管理?

问题描述:

mallocfree中完成的C内存分配/取消分配.

In C memory allocation/deallocation done by malloc and free.

在C ++中,由newdelete完成的内存分配/取消分配.

In C++ memory allocation/deallocation done by new and delete.

C ++中有一些用于自动内存管理的解决方案,例如:

There are some solutions in C++ for automatic memory management like:

  • 智能指针.
  • RAII(资源获取正在初始化)
  • 引用计数和循环引用
  • ...

但是我如何在C语言中进行自动内存管理?

But how can I do automatic memory management in C?

在C中是否有用于 AUTOMATIC 内存管理的解决方案?

Is there any solutions for AUTOMATIC memory management in C?

对于C是否有任何准则或类似准则?

Is there any guidelines or something like that for C?

当我放弃free一个内存块时,我想要:

I want when I foget free a block of memory:

  • 我的代码无法编译

-或-

  • 内存自动释放

然后我说: 哦,C比C ++,Java和C#好. :-)

And then I say: Oh, C is better than C++, Java and C#. :-)

您可以使用 Boehm垃圾收集器库.