处理用C内存分配的最佳方式? [英] Best way to handle memory allocation in C?

查看:107
本文介绍了处理用C内存分配的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我如何在C处理内存++但在C这样做是不同的,我有点不把握好。

I think I've got a good grasp on how to handle memory in C++ but doing it in C is different I'm a bit off.

在C ++中我有构造函数和析构函数,我已经得到了pretty简单的new和delete,我知道如何使用它RAII,智能指针和类中使用封装。

In C++ I've got constructors and destructors, I've got the pretty straightforward new and delete and I know how to encapsulate it using RAII, using with smart pointers and within classes.

然而,在C I不能处理malloc和free以同样的方式。我不知道如何隐藏,以及如何实现自动化的东西。所有我可以计算是使用函数发起并摧毁我的指针。但是,我应该怎么构建我的内存处理?

However in C I can't handle malloc and free the same way. I don't know how to hide them and how to automate things. All I can figure is using functions for initiating and destroying my pointers. But how should I structure my memory handling?

在写这一点,我已经意识到这更多的是关于我的问题理解的C比什么都重要的流动,但一次一个问题。

While writing this I've realized this is more a question about me understanding the flow of C than anything else, but one question at a time.

修改
感谢您的答案,但我需要改一下自己。

Edit: Thanks for the answers but I need to rephrase myself.

当我说我使用RAII和对C智能指针++我不想对C一样的,我知道这是不一样的。但是,我怎么在C ++处理内存分配连接到这些技术。

When I say that I use RAII and smart pointers for C++ I don't want the same for C, I know it's not the same. But how I handle memory allocation in C++ is connected to these techniques.

例如在我的班级我动态地添加和破坏我的课使用的内存。这样我可以实现一种封装的,我也不需要知道什么时候/如何/为什么类处理它的内存,它只是做。这意味着我可以隐藏低内存处理,只是集中于少数做大类。

For example in my classes I dynamically add and destroy the memory my class uses. This way I can achieve a sort of encapsulation, I don't need to know when/how/why the class handles it's memory, it just does. This means I can "hide" the lower memory handling and just focus on a few "bigger" classes.

我想知道的是什么是最好的做法在用C处理内存?没有与构造/析构函数来处理这对我来说没有课。它是很好的一个函数的开始分配内存或使用我创建它的函数?而且我应该怎么又释放他们?

What I want to know is what's the best-practice in handling memory in C? There are no classes with constructors/destructors to handle this for me. Is it good to allocate memory in the beginning of a function or use a function that creates it for me? And how should I free them again?

这是广泛的问题,他们从不同的情况的情况,但你怎么preFER处理呢?你能给什么秘诀和经验教训?

These are broad questions and they differ from situation to situation but how do you prefer to handle it? What tips and lessons can you give?

推荐答案

混乱的部分原因是,它本质上是在C更难的malloc 免费类似于删除的malloc 分配新的内存,并返回一个指向该内存。 免费使得内存再次可用,只要它是在用malloc分配的内存。否则,它只是使一些内存块的哈希值。它不关心。

Part of the confusion is that it is inherently more difficult in C. malloc and free are similar to new and delete: malloc allocates new memory, and returns a pointer to that memory. free makes that memory available again, so long as it's memory that was allocated using malloc. Otherwise, it just makes hash of some chunk of memory. It doesn't care.

使用malloc /免费的,重要的是决定并始终保持有纪律的使用。下面是一些提示:

The important thing with malloc/free is to decide on and consistently maintain a disciplined use. Here are some hints:

总是从检查的malloc返回的指针为NULL

ALWAYS check the returned pointer from malloc for NULL

if((p = (char *) malloc(BUFSIZ)) == NULL {
   /* then malloc failed do some error processing. */
}

有关腰带和背带安全,设置一个指向空释放后。

For belt and suspenders safety, set a pointer to NULL after freeing it.

free(p);
p = NULL ;

试着malloc和同一范围如果可能范围内的免费内存块:

try to malloc and free a chunk of memory within the same scope if possible:

 {  char * p ;
   if((p = malloc(BUFSIZ)) == NULL {
       /* then malloc failed do some error processing. */
   }

 /* do your work. */

   /* now you're done, free the memory */

   free(p);
   p = NULL ;  /* belt-and suspenders */
 }

当你不能,说清楚,什么你回来是的malloc '版内存,因此调用者可以释放它。

When you can't, make it clear that what you're returning is malloc'ed memory, so the caller can free it.

 /* foo: do something good, returning ptr to malloc memory */
 char * foo(int bar) {
     return (char *) malloc(bar);
 }

这篇关于处理用C内存分配的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆