问题描述
这是我的第一篇帖子,如果不合适,请原谅我。
我正在学习用C语言编程。已经差不多十年我了已经编程好了很多东西已经改变了。很多事情都发生了变化。
我从其他帖子中了解到来自malloc的结果
不好。在过去,我一直在铸造malloc。我认为这甚至是必要的。
。 (但那是很久以前的事了,这些天我听说过C的标准,我没有听说过。)是真的有必要还是做了?b $ b我们这样做只是为了让警告沉默? (cfr。常见问题解答q7.7 - 我不能这些日子看看手册中的
:我不再拥有它们了)
当我使用时malloc用于分配一小段内存,例如
a链接列表的单个节点,我使用''free''释放内存
之后。当使用''free''时,我不必担心指针所指向的已分配块的大小
。
是否是操作 - 系统负责记住那块内存的大小?
在这种情况下,当我通过分配内存构建一个大的链表时
$每个节点b $ b,我在操作系统中得到了很多开销。
这个对吗?或者这个特定于实现的操作系统是否特定于b $ b? (常见问题解答q7.26说实现''记得'
大小。这是否意味着每个实现都可以用他自己的
方式来实现?) />
如果计算机必须记住每个单独的分配大小
分配的块,是不是更好,分配更大的一块
当我尝试使用
来创建链表或二叉树或类似内容时,记忆并跟踪它在程序中的使用情况?
谢谢。
Joris Adriaenssens
This is my first posting, please excuse me if it is off-topic.
I''m learning to program in C. It''s been almost ten years I''ve been
programming and a lot of things have changed apparently.
I understand from other postings that casting a result from malloc
isn''t good. In the past I have always been casting the malloc. I
think it was even necessary. (But that''s a long time ago, I hadn''t
heard of a standard for C these days). Was it really necessary or did
we do it only to silence the warnings ? (cfr. FAQ q7.7 - I can''t
look in the manuals from these days : I don''t have them any longer)
When I use malloc for the allocation of a small piece of memory, e.g.
a single node of a linked list, I use ''free'' to release the memory
afterwards. When using ''free'', I don''t have to worry about the size
of the allocated block the pointer is pointing to.
Is it the operating-system that is responsible to remember the size of
that piece of memory ?
In that case, when I build a large linked-list by allocating memory
for each node, I get a lot of overhead in the operating system. Is
this right ? or is this implementation-specific or operating-system
specific ? (FAQ q7.26 says that the implementation ''remembers'' the
size. Does this mean that every implementation may do it in his own
way ? )
If the computer has to remember for each single allocation the size of
the allocated block, is it better than, to allocate a bigger piece of
memory and keep track of the use of it within the program, when I try
to create a linked-list or a binary tree or something like that ?
Thank you.
Joris Adriaenssens
推荐答案
"更好"也是一个意见问题。它也可能取决于你的系统的实现质量(由运行时库和操作系统确定)。
在具有好的系统中malloc,每个节点的单独调用可能更好(更少的代码编写,更容易维护等)。在一个贫穷的系统中。 malloc,一个大区域的单个调用你可能更喜欢(b $ b细分自己)(程序会明显运行
更快,更少内存碎片等)。
<<删除电子邮件的del>>
"better" is also a matter of opinion. It also probably depends on the
quality of implementation for your system (which is determined by both
the run-time library and the operating system).
In a system with a "good" malloc, individual calls for each node may
be preferable (less code to write, easier to maintain, etc). In a
system with a "poor" malloc, a single call for a large area which you
subdivide yourself may be preferable (program will run noticeably
faster, less fragmentation of memory, etc).
<<Remove the del for email>>
如果你认为你可以用比
实现更低的开销来进行跟踪,这可能是一个非常好的主意。
-
C有它的问题,但从头开始设计的语言也会有一些,
我们知道C'的问题。"
--Barne Stroustrup
If you think you can do the tracking with lower overhead than the
implementation, that can be a perfectly good idea.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C''s problems."
--Bjarne Stroustrup
最好的规则是使用malloc()来分配节点。然后,如果程序
运行得太慢,您可以看看是否可以优化。一个固定块分配器
可以比一个必须处理任何
大小的分配器更有效地编写。 Unfortunatley它使用的meory通常不能回收,因为它不是灵丹妙药。
The best rule is to use malloc() to allocate your nodes. Then if the program
runs too slowly, you can see if you can optimise. A fixed-block allocator
can be written much more efficiently than one that has to deal with any
size. Unfortunatley the meory it uses cannot normally be recycled for
general use, so it is not a panacea.
这篇关于malloc和免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!