堆栈和堆 [英] Stack and Heap

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

问题描述

大家好,

我需要对C ++中的内存分配做一些澄清。

我为冗长的解释道歉。


1.在C ++中,对象在堆中分配。

堆指的是什么?它是RAM /内存中的一个区域,还是将

引用到用于存储对象的数据结构。


2.在C ++中,函数及其局部变量进入堆栈。

如果作为基元的局部变量进入堆栈,则可以。但是什么

关于作为对象的局部变量。


2.1它们是堆还是堆栈?

2.1.1如果它们进入堆中,那么本地堆栈变量将如何获得对堆中对象的访问权限。编译器是否在内部维护任何对象

引用? (我已经读过Java中的内存分配,

其中对象引用存储在变量中,而不是对象本身的
。因此它完全没问题,因为本地参考

方法可以引用堆中的对象,因为它们有可用的引用

。)


2.1.2如果它们进入堆栈,那么对象可以驻留在堆栈中吗?


3.对于以下两种情况,2会产生相同的影响。


对象a;

对象b =新对象();


即对象a和b的内存分配是否相同。我理解由new创建的内存应该使用删除明确地销毁

。 [我们假设我没有超载新的

并删除]。但除此之外,对象a和b的内存分配是否存在任何差异。如果不是,那么

2中的规则将如何影响它们。


4.我读过C ++维护,2个内存位置来存储对象

即堆和免费商店。这有什么大不了的吗?


谢谢和问候,

Sarathy

解决方案

1。不对。用new创建的对象是在堆上创建的,其他所有对象都在堆栈中。你可能想要一些物品存在(通过

直接使用范围),所以你会使用新的


对象a; - 在堆栈上

对象* b =新对象; - 在堆上


2.所有变量除非使用new创建go go


通常如果你创建一个对象如下


对象* b =新对象。


指针b存在于堆栈中,但对象本身仍然存在于
$ b $中b堆。您需要确保删除对象的原因是,如果您的b $ b代码超出范围(最近的括号{}),则

指针b将不会更长的存在,你将留下一个对象

没有引用它的堆即内存泄漏


注意新的操作符返回指针到一个物体,所以你不能b / b



物体b =新物体;你必须使用


对象* b =新对象。


另请注意,如果使用默认构造函数创建对象

您可以使用
创建一个对象

对象* b =新对象;



对象* b =新的对象()。


希望这有帮助

sarathy写道:


大家好,

我需要对C ++中的内存分配做一些澄清。

我为冗长的解释道歉。


1.在C ++中,对象在堆中分配。

堆引用什么?它是RAM /内存中的一个区域,还是将

引用到用于存储对象的数据结构。


2.在C ++中,函数及其局部变量进入堆栈。

如果作为基元的局部变量进入堆栈,则可以。但是什么

关于作为对象的局部变量。


2.1它们是堆还是堆栈?

2.1.1如果它们进入堆中,那么本地堆栈变量将如何获得对堆中对象的访问权限。编译器是否在内部维护任何对象

引用? (我已经读过Java中的内存分配,

其中对象引用存储在变量中,而不是对象本身的
。因此它完全没问题,因为本地参考

方法可以引用堆中的对象,因为它们有可用的引用

。)


2.1.2如果它们进入堆栈,那么对象可以驻留在堆栈中吗?


3.对于以下两种情况,2会产生相同的影响。


对象a;

对象b =新对象();


即对象a和b的内存分配是否相同。我理解由new创建的内存应该使用删除明确地销毁

。 [我们假设我没有超载新的

并删除]。但除此之外,对象a和b的内存分配是否存在任何差异。如果不是,那么

2中的规则将如何影响它们。


4.我读过C ++维护,2个内存位置来存储对象

即堆和免费商店。有什么大不了的吗?


谢谢和问候,

Sarathy


< BLOCKQUOTE>" sarathy" < sp ********* @ gmail.comschrieb im Newsbeitrag

新闻:11 ********************* @ m79g2000cwm.googlegro ups.com ...


大家好,

我需要对C ++中的内存分配做一些澄清。 />
我为冗长的解释道歉。



首先,C ++不知道堆栈或堆,除了一些数据

类型,如std :: stack。 br />

基本上C ++定义了三种为变量分配内存的方法:

静态分配用于全局变量和静态变量。

自动分配用于函数内的局部变量。

免费存储用于分配new,malloc和类似

函数的变量。


实际上,堆栈通常用于实现自动分配和

有些人将内存称为可用于新堆的内存,但标准确实如此。
不需要使用堆栈进行自动分配。


1.在C ++中,对象在堆中分配。

堆引用什么至?它是RAM /内存中的一个区域,还是将

引用到用于存储对象的数据结构中。



在程序员希望分配C ++对象时分配它们。

如果他将对象定义为全局变量,则对象所有其他全局变量都分配了

;如果对象被定义为局部变量,那么对象的分配方式与任何其他局部变量一样;如果对象是使用new分配的
,则该对象将分配在由

new运算符提供的内存中。


2.在C ++中,函数及其局部变量进入堆栈。

如果作为基元的局部变量进入堆栈,则可以。但是关于作为对象的局部变量是什么?



堆栈,如果真的有一个,通常用于局部变量,

函数参数,返回地址和其他所需的数据保持

程序运行,但从不用于函数。函数可以使用堆栈(或者

分配任何局部变量),但它们永远不会是那个

内存的一部分。函数本身就是代码,通常代码和数据是分开的,至少在概念上是



在内存中变量'的数据将是存储取决于变量

的定义方式,而不取决于变量的类型。无论其类型如何,本地变量都是总是在堆栈上分配的
。 int'和任何

用户定义的类型以相同的方式处理。


与其他语言不同,C ++不区分值类型和

引用类型,它不区分原始类型和

对象。任何可以分配给某个变量的东西都是一个对象。


2.1它们是堆还是堆栈?



是。


2.1.1如果它们进入堆中,那么本地堆栈变量将如何获取

访问堆中的对象。编译器是否在内部维护任何对象

引用? (我已经读过Java中的内存分配,

其中对象引用存储在变量中,而不是对象本身的
。因此它完全没问题,因为本地参考

方法可以引用堆中的对象,因为它们具有可用的引用

。)



变量是一对,对象的名称和对象本身。对编译器(或其实现者)来说,编译器如何使用对象本身来识别对象的名称是



2.1.2如果它们进入堆栈,那么对象可以驻留在Stack中吗?



是。


3.对于以下2个案例,2会产生相同的效果。


对象a;

对象b =新对象();



这是Java或C#,但不是有效的C ++。 new返回一个指针,指针

只能存储在指针类型的变量中。


如果你想要一个本地对象,当它的名字变成时会被破坏

看不见,使用


对象a,


当你希望一个对象保持活着,即使执行离开范围

的分配,使用


对象* b =新对象;


即对象a和b的内存分配是否相同。我理解由new创建的内存应该使用删除明确地销毁

。 [我们假设我没有超载新的

并删除]。但除此之外,对象a和b的内存分配是否存在任何差异。如果不是,那么

2中的规则将如何影响它们。


4.我读过C ++维护,2个内存位置来存储对象

即堆和免费商店。有什么大不了的吗?



堆与堆之间没有太大区别和免费商店。更好地将

视为同一事物的两个不同名称。


最后一条建议。在编写C ++代码时,尽量忘记你所知道的关于Java的所有内容,特别是关于它的对象管理。它与你在C ++中所做的完全不同。


HTH

Heinz


在文章< 11 ********************* @ m79g2000cwm.googlegroups中。 com>,
sp*********@gmail.com 说。 ..


[...]


1.在C ++中,对象在堆中分配。

堆是指什么?它是RAM /内存中的一个区域,还是将

引用到用于存储对象的数据结构中。



C ++中的常用术语是从免费的

商店分配对象。 堆是指堆积。通常保留用于引用内存管理

with malloc / calloc / realloc / free。控制每个这些

的代码通常使用某种数据结构来管理内存,但是他们使用的

确切结构未被标准指定 - 但我不要回想起曾经看过或听说过使用堆数据结构的人。


2.在C ++中,函数和它的局部变量进入堆栈。



否 - 函数本身的内存通常是静态分配的。

即函数只是加载到内存中的某个地方,那个那是

的结尾。除了你可以获取

函数的地址并将其指定给函数的指针这一事实之外,C ++没有多少说明b $ b用于函数本身的内存。


本地变量以类似堆栈(LIFO)的方式分配/释放,所以

引用是常见的作为堆栈的那个内存,但C ++没有任何真正的要求,除了它一般的行为(即它是可用的

代码在函数开始执行时,函数返回后它不再是


如果局部变量是原语进入堆栈,没关系。但是什么

关于作为对象的局部变量。



他们怎么样?
< blockquote class =post_quotes>
2.1它们是堆还是堆栈?



如果你将它们声明为自动变量,它们会去在堆栈上(即

它们是自动管理的)。如果你将它们声明为全局对象,那么它们将被静态分配(通常)。


[...]


2.1.2如果它们进入堆栈,那么对象可以驻留在堆栈中吗?



正式如上所述,没有堆叠。非正式的,是的,

对象可以并且经常驻留在堆栈上。


3.对于以下2,2会产生相同的效果案例。


对象a;



这个效果取决于它的位置。如果它在一个

函数内部,那么它将是一个自动对象,这意味着它将存在于堆栈中

(在某种程度上存在一个)。


Object b = new Object();



这样做的效果更简单:它不会编译。 new返回一个指针

到一个对象,所以如果你要使用new,你分配它的结果需要是一个指针(除非你使用类型转换来强制它

到其他类型......)


对象* b =新对象;

会编译。这会在自动

存储中创建一个名为b的指针变量(即在堆栈中)。该变量使用在免费商店中创建的对象的

地址进行初始化。


注意:这些代码在编写良好的C ++中很常见。这通常是一个Java或Smalltalk程序员试图编写C ++的结果,而不是那些在C ++中有充分理由做的事情。

。 >


即对象a和b的内存分配是否相同。



是和否。 a是具有自动存储的对象。 b是一个带有

自动存储的指针,但是它所指向的对象具有动态存储。

因此a和b本身是相同分配的,但是a是一个

b是指针的对象,而对象b指向的对象与对象a完全分配




[...]


4.我读过C ++维护,2个内存位置来存储对象

即堆 ;和免费商店。有什么大不了的吗?



堆由malloc / calloc / realloc / free管理。使用new和delete管理免费商店

。有两个大不了:首先,不要尝试将
混合两者(例如,不要删除malloc'或免费的东西

新的东西。其次,你用malloc分配的是原始内存,而不是一个对象。你可以将它用于POD类(即一个类型,即b / b
将被C编译器识别),但不适用于任何类似于它们的类别

类型,例如任何类型的构造函数或任何虚函数。*


大多数C ++代码都是用C ++代码编写的(而不是C代码与'b
兼容) C ++编译器)从来没有任何理由在

all使用堆。免费商店的使用通常包含在一个专门用于管理该存储空间的类中(例如标准的

库的集合类) )。


1.对于任何看过的小伙子:是的,我知道你可以分配内存和

然后使用placement new在该内存中创建一个对象在堆上 -

但在这种情况下这可能会被忽略。


-

后来,

杰瑞。


宇宙是自己想象的虚构。


Hi all,
I need a few clarifications regarding memory allocaion in C++.
I apologize for the lengthy explanation.

1. In C++, Objects are allocated in heap.
What does heap refer to? Is it an area in RAM/Memory or does it refer
to a data structure being used for storing objects.

2. In C++, functions and its local variables go in stack.
If local variables that are primitives go in stack, it is OK. But what
about local variables that are objects.

2.1 Do they go in heap or in stack?
2.1.1 If they go in heap, then how will the local stack variable gain
access to an object in heap. Does the compiler maintain any object
reference internally? ( I have read about memory allocation in Java,
where in object references are being stored in the variable rather than
the object itself. Hence it is completely OK, since the local reference
in method can refer to an object in heap, since they have the reference
available with them.)

2.1.2 If they go in stack, then can objects reside in Stack?

3. Will 2 have the same effect on the following 2 cases.

Object a;
Object b = new Object();

i.e Is the memory allocation for both Object a and b same. I
understand that the memory created by "new" should be explicitly
destroyed using "delete". [Let us assume that i am not overloading new
and delete]. But other than that, is there any difference between
memory allocation for object a and b. If no, then how will the rules in
2 affect them.

4. I''ve read that C++ maintains, 2 memory locations to store objects
namely "heap" and "free store". Is there any big deal in it?

Thanks and regards,
Sarathy

解决方案

1. Not true. Objects created with new are created on the heap, all
other are on the stack. You may want some objects to live in ( passed
your immediate scope usage ) and so you would use new

Object a; - on stack
Object* b = new Object; - on heap

2. All variables unless created with new go on the stack

generally if you create an object as follows

Object* b = new Object.

The pointer b lives on the stack but the object itself lives on in the
heap. The reason you need to make sure you delete Objects is if you
code goes out of scope ( out of nearest set of braces {} ) then the
pointer b will no longer exist and you will be left with an object on
the heap that has no references to it i.e memory leak

note the new operator return you a pointer to an object so you can''t
do:

Object b = new Object; you must use

Object* b = new Object.

Also note that if you create an object using the default constructor
you may create an object using

Object* b = new Object;
or
Object* b = new Object().

Hope this helps
sarathy wrote:

Hi all,
I need a few clarifications regarding memory allocaion in C++.
I apologize for the lengthy explanation.

1. In C++, Objects are allocated in heap.
What does heap refer to? Is it an area in RAM/Memory or does it refer
to a data structure being used for storing objects.

2. In C++, functions and its local variables go in stack.
If local variables that are primitives go in stack, it is OK. But what
about local variables that are objects.

2.1 Do they go in heap or in stack?
2.1.1 If they go in heap, then how will the local stack variable gain
access to an object in heap. Does the compiler maintain any object
reference internally? ( I have read about memory allocation in Java,
where in object references are being stored in the variable rather than
the object itself. Hence it is completely OK, since the local reference
in method can refer to an object in heap, since they have the reference
available with them.)

2.1.2 If they go in stack, then can objects reside in Stack?

3. Will 2 have the same effect on the following 2 cases.

Object a;
Object b = new Object();

i.e Is the memory allocation for both Object a and b same. I
understand that the memory created by "new" should be explicitly
destroyed using "delete". [Let us assume that i am not overloading new
and delete]. But other than that, is there any difference between
memory allocation for object a and b. If no, then how will the rules in
2 affect them.

4. I''ve read that C++ maintains, 2 memory locations to store objects
namely "heap" and "free store". Is there any big deal in it?

Thanks and regards,
Sarathy


"sarathy" <sp*********@gmail.comschrieb im Newsbeitrag
news:11*********************@m79g2000cwm.googlegro ups.com...

Hi all,
I need a few clarifications regarding memory allocaion in C++.
I apologize for the lengthy explanation.

First of all, C++ does not know of a stack or heap, except for some data
types like std::stack.

Basically C++ defines three ways to allocate memory for variables:

Static allocation is used for global and static variables.
Automatic allocation is used for local variables inside functions.
The free store is used for variables allocated with new, malloc and similiar
functions.

As it happens, a stack is often used to implement automatic allocation and
some people call the memory available to new a heap, but the standard does
not require to use a stack for automatic allocation.

1. In C++, Objects are allocated in heap.
What does heap refer to? Is it an area in RAM/Memory or does it refer
to a data structure being used for storing objects.

In C++ objects are allocated as the programmer wants them to be allocated.
If he defines an object as a global variable, the object is allocated with
all other global variables; if the object is defined as a local variable,
the objects is allocated like any other local variable; and if the object is
allocated using new, the object is allocated in the memory provided by the
new operator.

2. In C++, functions and its local variables go in stack.
If local variables that are primitives go in stack, it is OK. But what
about local variables that are objects.

The stack, if there really is one, is commonly used for local variables,
function parameters, return addresses and other data needed to keep the
program running, but never for functions. Functions may use the stack (or
whatever local variables are allocated in), but they are never part of that
memory. A function itself is code, and usually code and data are kept apart,
at least conceptually.

Where in memory a variable''s data will be stored depends on how the variable
has been defined, but not on the type of the variable. A local variable is
always allocated "on the stack", regardless of its type. int''s and whatever
user defined type you are using are treated in exacly the same way.

Unlike other languages, C++ does not distinguish between value types and
reference types, and it does not distinguish between primitive types and
objects. Anything, that can be assigned to some variable, is an object.

2.1 Do they go in heap or in stack?

Yes.

2.1.1 If they go in heap, then how will the local stack variable gain
access to an object in heap. Does the compiler maintain any object
reference internally? ( I have read about memory allocation in Java,
where in object references are being stored in the variable rather than
the object itself. Hence it is completely OK, since the local reference
in method can refer to an object in heap, since they have the reference
available with them.)

A variable is a pair, the name of the object and the object itself. It is up
to the compiler (or its implementor) how the compiler assiciates the name of
the object with the object itself.

2.1.2 If they go in stack, then can objects reside in Stack?

Yes.

3. Will 2 have the same effect on the following 2 cases.

Object a;
Object b = new Object();

That is Java or C#, but not valid C++. new returns a pointer, and pointers
can only be stored in variables of pointer type.

If you want a local object that is destroyed when its name becomes
invisible, use

Object a,

When you want an object that stays alive even if execution leaves the scope
of its allocation, use

Object* b = new Object;

i.e Is the memory allocation for both Object a and b same. I
understand that the memory created by "new" should be explicitly
destroyed using "delete". [Let us assume that i am not overloading new
and delete]. But other than that, is there any difference between
memory allocation for object a and b. If no, then how will the rules in
2 affect them.

4. I''ve read that C++ maintains, 2 memory locations to store objects
namely "heap" and "free store". Is there any big deal in it?

There is no big difference between "heap" and "free store". Better think of
the as two differnt names for the same thing.

One last advice. When writing C++ code, try to forget everything you know
about Java, especially about its object management. It''s complety different
from what you are doing in C++.

HTH
Heinz


In article <11*********************@m79g2000cwm.googlegroups. com>,
sp*********@gmail.com says...

[ ... ]

1. In C++, Objects are allocated in heap.
What does heap refer to? Is it an area in RAM/Memory or does it refer
to a data structure being used for storing objects.

The usual terminology in C++ is that objects are allocated from the free
store. The "heap" is normally reserved for referring to memory managed
with malloc/calloc/realloc/free. The code that controls each of these
normally uses some sort of data structure to manage the memory, but the
exact structure they use is unspecified by the standard -- but I don''t
recall ever having seen or heard of one that used a heap data structure.

2. In C++, functions and its local variables go in stack.

No -- the memory for a function itself is normally statically allocated.
i.e. the function is simply loaded somewhere in memory, and that''s the
end of that. Other than the fact that you can take the address of a
function and assign it to a pointer to a function, C++ doesn''t have much
to say about the memory used for functions themselves.

Local variables are allocated/freed in a stack-like (LIFO) manner, so
it''s common to refer to that memory as the stack, but C++ doesn''t have
any real requirements beyond how it acts in general (i.e. it''s available
as soon as code in the function starts to execute, and it''s no longer
avaiable after the function returns.

If local variables that are primitives go in stack, it is OK. But what
about local variables that are objects.

What about them?

2.1 Do they go in heap or in stack?

If you declare them as automatic variables, they go on the stack (i.e.
they are managed automatically). If you declare them as global objects,
then they''ll be statically allocated (as a rule).

[ ... ]

2.1.2 If they go in stack, then can objects reside in Stack?

Officially, as noted above, there is no stack. Unofficially, yes,
objects can and often will reside on the stack.

3. Will 2 have the same effect on the following 2 cases.

Object a;

The effect of this depends on its location. If it''s inside of a
function, a will be an auto object, which means it''ll live on the stack
(to the extent that there is one).

Object b = new Object();

The effect of this is simpler: it won''t compile. new returns a pointer
to an object, so if you''re going to use new, the thing you assign its
result to needs to be a pointer (unless you use a type-cast to coerce it
to some other type...)

Object *b = new Object;

would compile. This creates a pointer variable named b in automatic
storage (i.e. on the stack). That variable is initialized with the
address of an Object created on the free store.

Note: such code is unusual in well-written C++. It''s usually the result
of a Java or Smalltalk programmer trying to write C++, not anything that
there was a good reason to do in C++.

i.e Is the memory allocation for both Object a and b same.

Yes and no. a is an Object with automatic storage. b is a pointer with
automatic storage, but the Object it''s pointing at has dynamic storage.
So a and b themselves are allocated simlilarly, but a is an Object where
b is a pointer, and the Object b is pointing at is allocated entirely
differently from the object a.

[ ... ]

4. I''ve read that C++ maintains, 2 memory locations to store objects
namely "heap" and "free store". Is there any big deal in it?

The heap is managed by malloc/calloc/realloc/free. The free store is
managed with new and delete. There are two "big deal"s: first, don''t try
to mix the two (e.g. don''t delete something that was malloc''d or free
something that was new''ed). Second, what you allocate with malloc is raw
memory, NOT an object. You can use it for POD classes (i.e. a type that
would be recognized by a C compiler), but not for any fancier class
types, such as anything with a constructor or any virtual functions.*

Most C++ code that''s really written as C++ code (rather than C that''s
compatible with a C++ compiler) never has any reason to use the heap at
all. Use of the free store is usually wrapped up in a class that''s
dedicated primarily to managing that piece of storage (e.g the standard
library''s collection classes).

1. For any pedants looking on: yes, I know you can allocate memory and
then use placement new to create an object in that memory on the heap --
but under the circumstances this is probably better ignored.

--
Later,
Jerry.

The universe is a figment of its own imagination.


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

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