malloc和免费 [英] malloc and free

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

问题描述

这是我的第一篇帖子,如果不合适,请原谅我。


我正在学习用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

推荐答案

2004年9月6日08:39:00 -0700,乔**************** @ hotmail.com (Joris

Adriaenssens)写道:
On 6 Sep 2004 08:39:00 -0700, jo****************@hotmail.com (Joris
Adriaenssens) wrote:
这是我的第一篇帖子,请原谅我是否偏离主题。

我从其他帖子中了解到,从malloc施放结果
并不好。在过去,我一直在铸造malloc。我认为这甚至是必要的。 (但那是很久以前的事了,这些天我没有听​​说过C的标准)。是真的有必要还是我们这样做只是为了让警告沉默? (cfr。常见问题q7.7 - 我不能在这些日子里查看手册:我再也没有了)


事情发生变化时间。当malloc

开始返回void *时,演员阵容变得不必要了。


使用演员来沉默警告是常见但不是万无一失的

技术。只要它正确编译一个符合要求的程序,编译器就可以自由地提供任何(多余的)诊断



当我使用malloc进行分配时一小段记忆,例如链接列表的单个节点,我使用''free''来释放内存
。当使用''free''时,我不必担心指针所指向的已分配块的大小。
操作系统是否负责记住大小那段记忆?


这是一项实施责任。是否落在C运行时库的操作系统上是一个实现问题。

在这种情况下,当我通过分配内存构建一个大的链表时
对于每个节点,我在操作系统中得到了很多开销。是


" lot"是一个意见问题。有些操作系统可能会这样做,因此对你来说有效的开销量是不可接受的*。

这对吗?或者这个特定于实现或操作系统是否具体? (FAQ q7.26说实现''记住'
大小。这是否意味着每个实现都可以用自己的方式来实现?)




如果计算机必须记住每个单独的分配大小
分配的块,是否比分配更大的内存和保持更好跟踪它在程序中的使用,当我尝试创建链表或二叉树或类似的东西时?
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)
Things change over time. The cast became unnecessary when malloc
started returning void*.

Using a cast to silence a warning is a common but not foolproof
technique. The compiler is free to give any (superfluous) diagnostic
it likes as long as it correctly compiles a conforming program.


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 ?
It is an implementation responsibility. Whether that falls on the
operating system of the C run-time library is an implementation issue.
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
"lot" is a matter of opinion. Some operating systems may do this so
efficiently that the amount of overhead is not unacceptable *to you*.
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 ? )
Yes
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 ?




"更好"也是一个意见问题。它也可能取决于你的系统的实现质量(由运行时库和操作系统确定)。


在具有好的系统中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>>


jo **************** @ hotmail.com (Joris Adriaenssens)写道:
jo****************@hotmail.com (Joris Adriaenssens) writes:
我从其他帖子中了解到,从malloc施放结果
并不好。在过去,我一直在铸造malloc。我认为这甚至是必要的。 (但那是很久以前的事了,这些天我没有听​​说过C的标准)。是真的有必要还是我们这样做只是为了让警告沉默? (cfr。常见问题q7.7 - 我不能在这些日子里查看手册:我再也没有了)


这是必要的使用预ANSI编译器。任何ANSI C89或更高版本的兼容

编译器都不需要转换

malloc()的结果。但是,如果您忘记了#include< stdlib.h>的
,通常会收到警告。在这种情况下,正确的解决方法是添加#include
,而不是添加演员。

当我使用malloc分配一小段内存时,例如,链接列表的单个节点,我使用免费来释放内存
。当使用''free''时,我不必担心指针所指向的已分配块的大小。
操作系统是否负责记住大小那段记忆?


有人必须(或多或少)。它可能是一个链接到你的程序或它可能是操作系统的库。

在这种情况下,当我通过分配内存来构建一个大的链表时/>对于每个节点,我在操作系统中得到了很多开销。这是对的吗?或者这个特定于实现或操作系统是否具体?


是的,两者。

(常见问题解答q7.26说实施''记得''大小。
这是否意味着每一个实现可以用他自己的方式吗?)


是的。

如果计算机必须记住每个单独的分配
当我尝试创建一个链表或者一个链接列表时,分配块的大小,是否优于,分配更大的内存并跟踪它在程序中的使用情况。树或类似的东西?
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)
It was necessary with pre-ANSI compilers. Casting the result of
malloc() is not necessary with any ANSI C89 or later compliant
compiler. However, you''ll normally get a warning if you forget
to #include <stdlib.h>. In that case, the correct fix is to add
the #include, not to add a cast.
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 ?
Somebody has to (more or less). It may be a library linked into
your program or it may be the operating system.
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 ?
Yes, both.
(FAQ q7.26 says that the implementation ''remembers'' the size.
Does this mean that every implementation may do it in his own
way ? )
Yes.
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 ?




如果你认为你可以用比

实现更低的开销来进行跟踪,这可能是一个非常好的主意。

-

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




" Joris Adriaenssens <乔**************** @ hotmail.com>写了

"Joris Adriaenssens" <jo****************@hotmail.com> wrote

我从其他帖子中了解到,从malloc施放结果
并不好。在过去,我一直在铸造malloc。我认为这甚至是必要的。 (但那是很久以前的事了,这些天我没有听​​说过C的标准)。是真的有必要还是我们这样做只是为了让警告沉默? (cfr。常见问题解答q7.7 - 我不能在这些日子里查看手册:我不再拥有它们了)

C暗示无效*另一种指针。 C ++没有。从malloc()转换返回的主要原因是允许代码在C ++下编译为
。这可能是必要的,甚至是不可取的,这取决于您工作的环境。 。
操作系统是否负责记住那段内存的大小?

操作系统只需按需分配和释放内存。在

练习中,它通常通过在从malloc()返回指针之前将块的大小存储在内存中来实现这一点。然而,一个奇怪的和美妙的系统可能会以不同的方式做事。
如果计算机必须记住每个单独的分配大小的分配块,是否优于当我尝试创建链接列表或二叉树或类似的东西时,分配更大的内存并在程序中跟踪它的使用?

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)
C implicitly casts a void * to another type of pointer. C++ does not. The
main reason for casting the return from malloc() is to allow the code to
compile under C++. This may be necessary or even undesireable, depending on
the environment in which you work. .
Is it the operating-system that is responsible to remember the size of
that piece of memory ?
The operating system just has to allocate and free memory on demand. In
practise it usually does this by storing the size of the block in the memory
immediately before the pointer returned from malloc(). However a weird and
wonderful system may do things differently.
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 ?



最好的规则是使用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和免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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