关于释放的记忆。 [英] Regarding released memory.

查看:53
本文介绍了关于释放的记忆。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在学习C ++并且有一个问题。


使用免费或删除我们可以释放内存。释放内存之后

这个

发布的内存将会去..它是否会回到操作系统?

请解释。


提前致谢。

Vijay

Hi All,

I am learning C++ and have one question.

Using free or delete we can release the memory. After releasing memory
where this
released memory will go.. Does it go to back operating system?
Please explain.

Thanks in advance.
Vijay

推荐答案

Vijay写道:
Vijay wrote:
大家好,

我正在学习C ++并且有一个问题。

使用free或delete我们可以释放内存。释放内存后,这个
发布的内存将会消失。它是否会返回操作系统?
请解释。
Hi All,

I am learning C++ and have one question.

Using free or delete we can release the memory. After releasing memory
where this
released memory will go.. Does it go to back operating system?
Please explain.




这是实施的具体内容。它可能会返回,也可能不会。

有时,它只会以块的形式返回,所以它只会在

整个块被释放后返回。 />



That''s specific to the implementation. It might go back or it might not.
Sometimes, it is only returned in blocks, so it will only go back when a
whole block has been freed.


Vijay写道:
Vijay wrote:
大家好,

我正在学习C ++并且有一个问题。

使用免费或删除我们可以释放内存。释放内存之后
这个内存将会发生..它是否会回到操作系统?
Hi All,

I am learning C++ and have one question.

Using free or delete we can release the memory. After releasing memory
where this
released memory will go.. Does it go to back operating system?




标准没有做任何承诺关于删除其他

的效果比它调用解除分配函数来释放内存。

发生的事情没有进一步说明。这为

实现留下了相当大的自由。


至于free(),它在C标准中定义。所有保证的是内存可用于进一步分配的

。然而,即使是没有被认真对待的
:实现通常需要许可证

才能将内存返回给操作系统,在这种情况下内存可能

被其他进程收购,因此它不再可用。

有些实现使用混合策略,例如,它们保留了一个内存池

提供哪些小对象的分配,而大的

对象通过系统调用提供给底层操作系统。


对于好的代码,不要依赖于关于什么新的/删除或

malloc / free实际上会做什么。将它们视为抽象编程原语

用于内存管理。

最佳


Kai-Uwe Bux

ps。:如果您正在学习C ++,我建议您首先关注使用

std :: string以及标准容器和迭代器。对于

好​​的衡量标准,这将减少您手动管理内存的需求。一旦你学会了编写异常安全代码,就回到

内存管理。

例外 - 许多

陷阱和内存管理的困难严重加剧 - 因为每个新的必须匹配一个且只有一个删除

在流量控制的每一条可能的路径上,流量控制的转移导致了例外,这使得生活变得更加艰难。 C ++中的显式内存管理非常困难,你不应该不必要地开始使用C ++编程中最困难的一个方面:你不必要地学习曲线

陡峭。



The standard does not make any promisses about the effects of delete other
than that it calls a deallocation function to deallocate the memory. What
happens is not further specified. This leaves considerable freedom to the
implementation.

As for free(), it is defined in the C standard. All that is guaranteed is
that the memory becomes available for further allocation. However, even
that is not to be taken seriously: implementations often take the license
to return the memory to the operating system, in which case the memory may
be acquired by other processes so that it is not available anymore.
Some implementations use mixed strategies, e.g., they keep a pool of memory
around from which allocations of small objects are served whereas big
objects are served via system calls to the underlying OS.

For good code, do not rely on assumptions about what new/delete or
malloc/free will actually do. Treat them as abstract programming primitives
for memory management.
Best

Kai-Uwe Bux

ps.: If you are learning C++, I would recommend that you focus on the use of
std::string and standard containers and iterators first. This will, for
good measure, reduce your needs to manage memory manually. Come back to
memory management once you learned about writing exception safe code. Many
traps and difficulties with memory management are seriously aggravated by
exceptions -- since every new must match with one and only one delete along
every possible path of flow control, the diversions of flow control induced
by exceptions make life harder. Explicit memory management in C++ is
difficult and you should not needlessly start out with one of the hardest
aspects of C++ programming: you are make the learning curve unnecessarily
steep.


Kai-Uwe Bux写道:
Kai-Uwe Bux wrote:
Vijay写道:
Vijay wrote:
大家好,

我正在学习C ++并且有一个问题。

使用free或delete我们可以释放内存。释放内存之后
这个内存将会发生..它是否会回到操作系统?
Hi All,

I am learning C++ and have one question.

Using free or delete we can release the memory. After releasing memory
where this
released memory will go.. Does it go to back operating system?



该标准没有任何关于效果的承诺删除其他
而不是它调用解除分配函数来释放内存。
发生的事情没有进一步说明。这为
实现留下了相当大的自由。

对于free(),它在C标准中定义。所有保证的是内存可用于进一步分配。然而,即使不被认真对待:实现通常需要许可证才能将内存返回给操作系统,在这种情况下,内存可能被其他进程获取,以便它不再可用。

有些实现使用混合策略,例如,它们保留一个内存池,从中提供小对象的分配,同时提供大的
对象通过系统调用底层操作系统。

为了获得良好的代码,不要依赖于假设新的/删除或者malloc / free实际上会做什么。将它们视为抽象编程原语进行内存管理。

最佳

Kai-Uwe Bux

ps。:如果你正在学习C ++,我建议你首先关注st / :: string和标准容器和迭代器的使用。这样做可以减少手动管理内存的需求。一旦您学会了编写异常安全代码,请回到内存管理。
异常严重加剧了许多陷阱和内存管理的困难 - 因为每个新的必须匹配一个且只有一个删除沿着每个可能的流量控制路径,流量控制的转移通过例外诱导使生活变得更加艰难。 C ++中的显式内存管理非常困难,你不应该从C ++编程的最难的方面开始:你不必要地使学习曲线陡峭。



The standard does not make any promisses about the effects of delete other
than that it calls a deallocation function to deallocate the memory. What
happens is not further specified. This leaves considerable freedom to the
implementation.

As for free(), it is defined in the C standard. All that is guaranteed is
that the memory becomes available for further allocation. However, even
that is not to be taken seriously: implementations often take the license
to return the memory to the operating system, in which case the memory may
be acquired by other processes so that it is not available anymore.
Some implementations use mixed strategies, e.g., they keep a pool of memory
around from which allocations of small objects are served whereas big
objects are served via system calls to the underlying OS.

For good code, do not rely on assumptions about what new/delete or
malloc/free will actually do. Treat them as abstract programming primitives
for memory management.
Best

Kai-Uwe Bux

ps.: If you are learning C++, I would recommend that you focus on the use of
std::string and standard containers and iterators first. This will, for
good measure, reduce your needs to manage memory manually. Come back to
memory management once you learned about writing exception safe code. Many
traps and difficulties with memory management are seriously aggravated by
exceptions -- since every new must match with one and only one delete along
every possible path of flow control, the diversions of flow control induced
by exceptions make life harder. Explicit memory management in C++ is
difficult and you should not needlessly start out with one of the hardest
aspects of C++ programming: you are make the learning curve unnecessarily
steep.




感谢您提供如此宝贵的信息。


另外一个疑问。


我已经读了一个电子书新的是从''免费的

存储'中分配内存,而malloc是从堆中分配的。


是不是?

如果是,那么免费存储和堆之间有什么区别?

问候,

Vijay



Thanks for so valuable information.

One other doubt.

I have read in one e-book that new is allocating memory from ''free
storage'' and malloc is allocating from heap.

Is it right?
If yes, then what is difference between free storage and heap?
Regards,
Vijay


这篇关于关于释放的记忆。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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