C ++对象,堆栈还是堆? [Noob问题] [英] C++ Objects, Stack or Heap? [Noob question]

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

问题描述

在以下C ++课程中,


MyClass {

//成员

}

我从中创建两个对象(使用默认构造函数),

(1)MyClass * object1 = new MyClass(); // Stack

(2)MyClass object2 = MyClass(); //堆栈还是堆?


是否在堆上创建了对象(1)和(2),或者只是(1)?我怀疑

就是这种情况,但我不是百分百肯定因为我没有在任何地方看到这个




谢谢,


- Olumide

In the following C++ class,

MyClass{
// Members
}
From which I create two objects (using the default constructor),
(1) MyClass *object1 = new MyClass(); // Stack
(2) MyClass object2 = MyClass(); // Stack or Heap?

Are both objects (1) and (2) created on the heap or is (1) only? I suspect
this is the case, but I''m not 100% sure because I have not read this
anywhere.

Thanks,

- Olumide

推荐答案

Olumide写道:
Olumide wrote:
在下面的C ++类中,

MyClass {
//成员
}

我从中创建两个对象(使用默认值) (1)MyClass * object1 = new MyClass(); // Stack
(2)MyClass object2 = MyClass(); //堆栈还是堆?

堆上是否创建了对象(1)和(2),或者只是(1)?我怀疑是这样的,但我不是百分百肯定,因为我没有在任何地方看过这个

谢谢,

- Olumide
In the following C++ class,

MyClass{
// Members
}
From which I create two objects (using the default constructor),
(1) MyClass *object1 = new MyClass(); // Stack
(2) MyClass object2 = MyClass(); // Stack or Heap?

Are both objects (1) and (2) created on the heap or is (1) only? I suspect
this is the case, but I''m not 100% sure because I have not read this
anywhere.

Thanks,

- Olumide




MyClass * object1指针是在堆栈上创建的,但是它指向的对象是在堆上创建的。也就是说,指针自动被销毁,但是堆上的对象不是(需要删除

object1,即删除堆对象1上的对象指向)。


MyClass对象2是在堆栈上创建的对象。


因此,创建像object2这样的许多(大)对象可以导致堆栈溢出。


Baalbek



The MyClass *object1 pointer is created on the stack, but the object it
points to is created on the heap. That is, the pointer is automatically
destroyed, but the object on the heap is not (necessitating "delete
object1", that is deleting the object on the heap object1 points to).

MyClass object2 is an object created on the stack.

So, creating many (large) objects like object2 can lead to stack-overflow.

Baalbek


Olumide写道:
Olumide wrote:
在以下C ++类中,

类MyClass {
//成员
};

我从中创建两个对象(使用默认值) (1)MyClass * pobject1 = new MyClass(); //免费存储
(2)MyClass object2 = MyClass(); //自动存储

对象(1)和(2)都是在堆上创建的还是仅仅(1)?
我怀疑是这种情况,
但是我我不是100%肯定,因为我没有在任何地方看过这个。
In the following C++ class,

class MyClass{
// Members
};
From which I create two objects (using the default constructor),
(1) MyClass *pobject1 = new MyClass(); // Free Storage
(2) MyClass object2 = MyClass(); // Automatic Storage

Are both objects (1) and (2) created on the heap or is (1) only?
I suspect this is the case,
but I''m not 100% sure because I have not read this anywhere.




你错了。

存储从*分配对象的免费存储空间*第一种情况下pobject1指向的
和/ b
存储是从*自动存储*分配给对象2

in第二种情况。


在*典型实现*中,

*程序堆栈*用于自动存储和

a *免费列表*用于管理免费存储。

异想天开的术语* heap *是IBM程序员使用的双关语

来对比他们对免费列表的实现

带有堆栈数据结构。



You are wrong either way.
Storage is allocated from *free storage* for the object
to which pobject1 points in the first case and
storage is allocated from *automatic storage* for object2
in the second case.

In the *typical implementation*,
the *program stack* is used for automatic storage and
a *free list* is used to manage free storage.
The whimsical term *heap* is a pun used by IBM programmers
to contrast their implementation of a free list
with a stack data structure.


你区域的Java家伙吧? :-P


更好的是你读过像Thinking in C ++这样的东西。免费提供

在网上。你对C ++的看法有些混淆(用Java)。

You area Java guy huh? :-P

Better that you read something like "Thinking in C++" freely available
on the web. Your ideas about C++ are somewhat confused (by Java).

我从中创建两个对象(使用默认构造函数),
(1)MyClass * object1 = new MyClass(); //堆栈


^^^^堆栈^^^^堆


^^^一个对象


^^^一个指针,而不是一个对象


^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^

记得要做

删除object1;

某个点,下面,当你不再需要它时。你在C ++中没有

垃圾收集器!!


(2)MyClass object2 = MyClass(); //堆栈还是堆?
From which I create two objects (using the default constructor),
(1) MyClass *object1 = new MyClass(); // Stack
^^^^ stack ^^^^ heap

^^^ an object

^^^ a pointer,not an object

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
remember to do
delete object1;
at a certain point, below, when you don''t need it anymore. You have no
garbage collector in C++!!

(2) MyClass object2 = MyClass(); // Stack or Heap?




^^^^堆栈^^^^堆栈

^^^一个对象

^^^一个对象(临时和

立即销毁)

^^^从右侧临时复制的对象值


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^不要做这个(2)用C ++写的东西! !

简单地说:

MyClass object2(....可能参数...);



^^^^ stack ^^^^ stack
^^^ an object
^^^ an object (temporary and
immediately destroyed)
^^^ value of object copied from the temporary on the right

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ DON''T DO THIS (2) STUFF IN C++!!
do simply:
MyClass object2(....possible params...);


这篇关于C ++对象,堆栈还是堆? [Noob问题]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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