C++ 使用 new 和不使用实例化对象有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3673998/
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
What is difference between instantiating an object using new vs. without
提问by manzy704
In C++,
在 C++ 中,
Aside from dynamic memory allocation, is there a functional difference between the following two lines of code:
除了动态内存分配之外,以下两行代码之间是否存在功能差异:
Time t (12, 0, 0); //t is a Time object
Time* t = new Time(12, 0, 0);//t is a pointer to a dynamically allocated Time object
I am assuming of course that a Time(int, int, int) ctor has been defined. I also realize that in the second case t will need to be deleted as it was allocated on the heap. Is there any other difference?
我当然假设已经定义了一个 Time(int, int, int) ctor。我也意识到在第二种情况下 t 需要删除,因为它是在堆上分配的。还有其他区别吗?
回答by greyfade
The line:
线路:
Time t (12, 0, 0);
... allocates a variable of type Time
in local scope, generallyon the stack, which will be destroyed when its scope ends.
...Time
在局部范围内分配类型的变量,通常在堆栈上,当其范围结束时将被销毁。
By contrast:
相比之下:
Time* t = new Time(12, 0, 0);
... allocates a block of memory by calling either ::operator new()
or Time::operator new()
, and subsequently calls Time::Time()
with this
set to an address within that memory block (and also returned as the result of new
), which is then stored in t
. As you know, this is generallydone on the heap (by default) and requires that you delete
it later in the program, while the pointer in t
is generallystored on the stack.
...通过调用分配的内存块::operator new()
或Time::operator new()
,并随后调用Time::Time()
与this
集到该存储块内的地址(以及返回的结果new
),然后将其存储在t
。如你所知,这是一般的堆(默认)完成,并要求您delete
在以后的节目,而指针t
则一般存储在堆栈上。
回答by Manoj R
One more obvious difference is when accessing the variables and methods of t.
一个更明显的区别是在访问 t 的变量和方法时。
Time t (12, 0, 0);
t.GetTime();
Time* t = new Time(12, 0, 0);
t->GetTime();
回答by Ates Goral
As far as the constructor is concerned, the two forms are functionally identical: they'll just cause the constructor to be called on a newly allocated object instance. You already seem to have a good grasp on the differences in terms of allocation modes and object lifetimes.
就构造函数而言,这两种形式在功能上是相同的:它们只会导致在新分配的对象实例上调用构造函数。您似乎已经很好地掌握了分配模式和对象生命周期方面的差异。
回答by taskinoor
I think you already understand all the differences. Assuming that you are well aware about the syntax difference of accessing a member of t through a pointer and through a variable (well, pointer is also a variable but I guess you understand what I mean). And assuming also that you know the difference of call by value and call by reference when passing t to a function. And I think you also understand what will happen if you assign t to another variable and make change through that other variable. The result will be different depending on whether t is pointer or not.
我想你已经明白所有的区别了。假设您很清楚通过指针和变量访问 t 成员的语法差异(好吧,指针也是一个变量,但我想您明白我的意思)。并假设您在将 t 传递给函数时知道按值调用和按引用调用的区别。而且我认为您也了解如果将 t 分配给另一个变量并通过该其他变量进行更改会发生什么。根据 t 是否为指针,结果会有所不同。
回答by Christopher Hunt
There is no functional difference to the object between allocating it on the stack and allocating it on the heap. Both will invoke the object's constructor.
在堆栈上分配对象和在堆上分配对象之间在功能上没有区别。两者都将调用对象的构造函数。
Incidentally I recommend you use boost's shared_ptr or scoped_ptr which is also functionally equivalent when allocating on the heap (with the additional usefulness of scoped_ptr constraining you from copying non-copyable pointers):
顺便说一下,我建议您使用 boost 的 shared_ptr 或 scoped_ptr,它们在在堆上分配时在功能上也是等效的(scoped_ptr 的额外用处是限制您复制不可复制的指针):
scoped_ptr<Time> t(new Time(12, 0, 0));
回答by Learner
There is no other difference to what you know already.
与你已经知道的没有其他区别。
Assuming your code is using the service of default operator new.
假设您的代码正在使用默认运营商 new 的服务。
回答by haibo cu
- Use new: Call operator new function to get dynamic memory, and then to call the constuctor function.
- Not use new: Will not call operator new function, just directly to call the constuctor function. The stack will be used directly, no use to malloc.
- 使用 new: 调用 operator new 函数获取动态内存,然后调用构造函数。
- 不使用new:不会调用operator new函数,直接调用constructor函数。栈会直接使用,没用到malloc。
回答by fangzhzh
void foo (Time t)
{
t = Time(12, 0, 0);
}
void bar (Time* t)
{
t = new Time(12, 0, 0);
}
int main(int argc, char *argv[])
{
Time t;
foo(t);//t is not (12,0,0),its value depends on your defined type Time's default constructor.
bar(&t);//t is (12,0,0)
return 0;
}