C++ 在堆栈/堆上创建对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10157122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object creation on the stack/heap?
提问by user997112
The following code creates an object on the stack:
以下代码在堆栈上创建一个对象:
Object o;
When creating an object on the heap we can use:
在堆上创建对象时,我们可以使用:
Object* o;
o = new Object();
rather than:
而不是:
Object* o = new Object();
When we split the heap object-creation over two lines and call the constructor on the second line (o = new object()
), does this mean in the first line (Object* o
) the pointer was created on the stack? So Object o
puts the object on the stack, whereas Object* o
puts the pointer to a future object on the stack?
当我们将堆对象创建分成两行并在第二行 ( o = new object()
)上调用构造函数时,这是否意味着在第一行 ( Object* o
) 中,指针是在堆栈上创建的?那么Object o
将对象放在堆栈上,而Object* o
将指向未来对象的指针放在堆栈上?
My second question involves if the two lines of code were called outside of a class. I recently read (Global memory management in C in stack or heap?) that global variables are not contained on the stack/heap but actually another part of memory? If this is the case, would Object* o
create a pointer which would sit in this other part of the memory and it points to the heap object?
我的第二个问题涉及这两行代码是否是在类之外调用的。我最近读到(堆栈或堆中 C 中的全局内存管理?)全局变量不包含在堆栈/堆中,而实际上是内存的另一部分?如果是这种情况,是否会Object* o
创建一个指针,该指针将位于内存的另一部分并指向堆对象?
回答by Konrad Rudolph
Actually, neither statement says anything about heap or stack:
实际上,这两个语句都没有说明堆或堆栈:
Object o;
creates an object with automaticstorage meaning that the storage location is determined by the context in which the object is declared: If the code is in a function, this happens to be the call stack. But the line could also be a class member or, as you've noted, outside of a function / class.
创建一个具有自动存储的对象,这意味着存储位置由声明对象的上下文决定:如果代码在函数中,这恰好是调用堆栈。但该行也可以是类成员,或者如您所见,在函数/类之外。
To illustrate why this is different:
为了说明为什么这是不同的:
struct Foo {
Object o;
};
Foo* pf = new Foo();
Now the object pf->o
is created on the heap, not on the stack, even though (or rather, because) it has automatic storage.
现在对象pf->o
是在堆上创建的,而不是在堆栈上,即使(或者更确切地说,因为)它有自动存储。
Conversely,
反过来,
Object* p;
simply declares a pointer, nothing more. The pointer's storage is indistinguishable from any other object's: it has automatic storage. Furthermore, the initialising expression has no effect on the variable storage.
只是声明一个指针,仅此而已。的指针的存储是从任何其他对象的不可区分的:它具有自动存储。此外,初始化表达式对变量存储没有影响。
What the pointer points to is a completely different matter. It might be a heap-allocated object (using new
for instance) or it might point to another automatically allocated object. Consider:
指针指向什么是完全不同的事情。它可能是一个堆分配的对象(new
例如使用),或者它可能指向另一个自动分配的对象。考虑:
Object o;
Object* p = &o;
回答by DML
C++ offers three different ways to create objects:
C++ 提供了三种不同的方式来创建对象:
- Stack-based such as temporary objects
- Heap-based by using new
- Static memory allocation such as global variables and namespace-scope objects
- 基于栈的比如临时对象
- 使用新的基于堆的
- 静态内存分配,例如全局变量和命名空间范围的对象
Consider your case,
考虑你的情况,
Object* o;
o = new Object();
and:
和:
Object* o = new Object();
Both forms are the same. This means that a pointer variable o is created on the stack (assume your variables does not belong to the 3 category above) and it points to a memory in the heap, which contains the object.
两种形式都是一样的。这意味着在堆栈上创建了一个指针变量 o(假设您的变量不属于上述 3 类),它指向堆中包含对象的内存。
回答by geekosaur
The two forms are the same with one exception: temporarily, the new (Object *)
has an undefined value when the creation and assignment are separate. The compiler may combine them back together, since the undefined pointer is not particularly useful. This does not relate to global variables (unless the declaration is global, in which case it's still true for both forms).
这两种形式是相同的,但有一个例外:(Object *)
当创建和赋值分开时, new 暂时具有未定义的值。编译器可能会将它们重新组合在一起,因为未定义的指针不是特别有用。这与全局变量无关(除非声明是全局的,在这种情况下,这两种形式仍然适用)。
回答by user966379
A)
一种)
Object* o;
o = new Object();
`` B)
``乙)
Object* o = new Object();
I think A and B has no difference. In both the cases o is a pointer to class Object. statement new Object() creates an object of class Object from heap memory. Assignment statement assigns the address of allocated memory to pointer o.
我认为A和B没有区别。在这两种情况下,o 都是指向 Object 类的指针。语句 new Object() 从堆内存中创建一个 Object 类的对象。赋值语句将分配的内存地址分配给指针o。
One thing I would like to mention that size of allocated memory from heap is always the sizeof(Object) not sizeof(Object) + sizeof(void *).
我想提到的一件事是,从堆分配的内存大小始终是 sizeof(Object) 而不是 sizeof(Object) + sizeof(void *)。
回答by atlascoder
C++ has Automatic variables - not Stack variables.
C++ 有自动变量——而不是堆栈变量。
Automatic variable means that C++ compiler handles memory allocation / free by itself. C++ can automatically handle objects of any class - no matter whether it has dynamically allocated members or not. It's achieved by strong guarantee of C++ that object's destructor will be called automatically when execution is going out of scope where automatic variable was declared. Inside of a C++ object can be a lot of dynamic allocations with new
in constructor, and when such an object is declared as an automatic variable - all dynamic allocations will be performed, and freed then in destructor.
自动变量意味着 C++ 编译器自己处理内存分配/释放。C++ 可以自动处理任何类的对象——不管它是否有动态分配的成员。它是通过 C++ 的强大保证实现的,当执行超出声明自动变量的范围时,将自动调用对象的析构函数。在 C++ 对象内部可以有很多动态分配,new
在构造函数中,当这样的对象被声明为自动变量时 - 将执行所有动态分配,然后在析构函数中释放。
Stack variables in C can't be dynamically allocated. Stack in C can store pointers, or fixed arrays or structs - all of fixed size, and these things are being allocated in memory in linear order. When a C program frees a stack variable - it just moves stack pointer back and nothing more.
C 中的堆栈变量不能动态分配。C 中的堆栈可以存储指针、固定数组或结构——所有的大小都是固定的,这些东西在内存中以线性顺序分配。当 C 程序释放堆栈变量时 - 它只是将堆栈指针移回,仅此而已。
Even though C++ programs can use Stack memory segment for storing primitive types, function's args, or other, - it's all decided by C++ compiler, not by program developer. Thus, it is conceptually wrong to equal C++ automatic variables and C stack variables.
尽管 C++ 程序可以使用 Stack 内存段来存储原始类型、函数的参数或其他,但这都是由 C++ 编译器决定的,而不是由程序开发人员决定的。因此,将 C++ 自动变量和 C 堆栈变量相等在概念上是错误的。
回答by dasblinkenlight
In both your examples, local variables of Object*
type are allocated on the stack. The compiler is free to produce the same code from both snippets if there is no way for your program to detect a difference.
在您的两个示例中,Object*
类型的局部变量都在堆栈上分配。如果您的程序无法检测差异,则编译器可以自由地从两个片段生成相同的代码。
The memory area for global variables is the same as the memory area for static variables - it's neither on the stack nor on the heap. You can place variables in that area by declaring them static
inside the function. The consequence of doing so is that the instance becomes sharedamong concurrent invocations of your function, so you need to carefully consider synchronization when you use statics.
全局变量的内存区域与静态变量的内存区域相同 - 它既不在堆栈上也不在堆上。您可以通过static
在函数内声明变量来将变量放置在该区域中。这样做的结果是实例在函数的并发调用之间共享,因此在使用静态时需要仔细考虑同步。
Here is a linkto a discussion of the memory layout of a running C program.
这是有关正在运行的 C 程序的内存布局的讨论的链接。