如何避免使用malloc? [英] Ways to avoid using malloc?

查看:176
本文介绍了如何避免使用malloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着近年来所有的喧哗Ç得到,我看了,有一些方法,尽量减少用C的malloc的使用,而且它是一个很好的做法。我怎么过不知道何时或怎样还是怎样这种做法是很好的。所以我的问题是,也许一些有经验的C程序员可以举一些例子,其中一个可能(或应该)乱写malloc的东西,但什么是为新手C程序员真的非显而易见的方法(因此说,新手只会用malloc) ?也许你从分解出的malloc成别的东西有一些经验。

With all recent noise C gets, I read that there are some ways to minimize usage of malloc in C, and that it is a very good practice. I how ever have no idea when or how or what such practices are good. So my question would be, maybe some experienced C programmers could give some examples where one could (or should) write something without malloc, but what would be really non-obvious way for newbie C programmer (and thus said newbie would simply use malloc)? Maybe you have some experiences from factoring out malloc into something else.

P.S。一些帖子,我看到被引用雷神之锤3源$ C ​​$ c和如何避免使用malloc,所以如果有人有这方面的知识,将是有趣的,知道有什么做的,因为至少知道我想避免挖到漫无目的地颤抖code。 (因为好,如果他们避免使用malloc搜索的malloc不会给太多的结果,我想,也code基是最有可能不是那么简单,例如个人可以)

P.S. some posts I read were referencing Quake 3 source code and how it avoids use of malloc, so if someone has knowledge of this it would be interesting to know what is done there, since at least for know I would like to avoid digging into quakes code aimlessly. (since well if they avoid using malloc searching for malloc will not give much results I suppose, also code base is most likely not as simple as individual examples could be)

推荐答案

我不知道完全避免的malloc ,但你肯定可以减少它。

I don't know about totally avoiding malloc, but you can certainly reduce it.

的基本概念是存储器池。这是你分配你可以使用许多对象,而不是请求许多小分配的一个大的缓冲区。

The basic concept is a memory pool. That is a large buffer which you have allocated that you can use for many objects instead of requesting lots of small allocations.

您可能会在真实世界的情况下,你要发送到的事件队列被另一个线程处理使用。本次活动的对象可能是小巧的结构和你真的需要避免使数以千计的电话,以的malloc 每一秒。

You might use this in a real-world situation where you are sending events into a queue to be processed by another thread. The event objects might be smallish structures and you really need to avoid making thousands of calls to malloc every second.

答案当然是从池中提取这些事件对象。如果你需要,你甚至可以用您的缓冲池的部分,形成一个列表,以便您可以快速索引内存已经返回到池中。这些通常被称为的自由列表

The answer of course is to draw these event objects from a pool. If you need to, you can even use parts of your pool buffer to form a list so that you can quickly index memory that has been returned to the pool. These are generally known as free-lists.

你必须要小心内存对齐,因为你可以通过严重错位有数据影响性能。但是你可以处理所有的一点点数学。

You do have to be careful about memory alignment, as you can severely impact performance by having misaligned data. But you can handle all that with a little maths.

不要吓坏了这些概念。池实际上并不要那么复杂。试想一下:

Don't freak out about these concepts. A pool doesn't actually have to be that sophisticated. Consider this:

int ** matrix = malloc( rows * sizeof(int*) );
for( int i = 0; i < rows; i++ ) {
    matrix[i] = malloc( cols * sizeof(int) );
}

我看到这一切的时候,这是我的一个忌讳。为什么你要这么做时,你可以这样做:

I see this all the time, and it's a pet peeve of mine. Why would you do that, when you can do this:

int ** matrix = malloc( rows * sizeof(int*) );
matrix[0] = malloc( rows * cols * sizeof(int) );
for( int i = 1; i < rows; i++ ) {
    matrix[i] = matrix[i-1] + cols;
}

当然,降低到这一点(您的第一排,虽然提防潜在的对齐问题 - 我在这里忽略了它为清楚起见)

And of course, that reduces to this (beware of potential alignment issues in your first row though - I've ignored it here for the sake of clarity)

int ** matrix = malloc( rows * sizeof(int*) + rows * cols * sizeof(int) );
matrix[0] = (int*)matrix + rows;
for( int i = 1; i < rows; i++ ) {
    matrix[i] = matrix[i-1] + cols;
}

有关最后一个例子最酷的是它是多么容易删除矩阵=)

The cool thing about that last example is how easy it is to delete your matrix =)

free( matrix );

哦,零矩阵一样简单...

Oh, and zeroing the matrix is just as easy...

memset( matrix[0], 0, rows * cols * sizeof(int) );

这篇关于如何避免使用malloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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