使用new和不使用new实例化对象有什么区别

时间:2022-10-19 16:18:21

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?

当然,我假设一个时间(int, int) ctor已经被定义。我还意识到,在第二种情况下,当在堆上分配t时,需要删除它。还有其他的区别吗?

9 个解决方案

#1


107  

The line:

线:

Time t (12, 0, 0);

... allocates a variable of type Time in local scope, generally on the stack, which will be destroyed when its scope ends.

…在局部范围内分配类型时间的变量,通常在堆栈上,在其范围结束时将被销毁。

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 generally done on the heap (by default) and requires that you delete it later in the program, while the pointer in t is generally stored on the stack.

…分配一块内存通过调用::运营商新的()或时间::运营商新(),并随后调用时间:时间()这组内的一个地址,内存块(也作为结果返回新的),然后存储在t。如你所知,这通常是在堆(默认情况下),并要求您删除它在之后的计划,而在t通常存储在堆栈的指针。

#2


24  

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();

#3


5  

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.

就构造函数而言,这两种形式在功能上是完全相同的:它们只会导致在新分配的对象实例上调用构造函数。您似乎已经很好地理解了分配模式和对象生存期之间的差异。

#4


3  

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是否为指针而不同。

#5


1  

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));

#6


1  

No.. There is no other difference..

没有. .没有其他的区别。

#7


1  

There is no other difference to what you know already.

你所知道的没有其他的差别。

Assuming your code is using the service of default operator new.

假设您的代码正在使用默认操作符new的服务。

#8


1  

  • Use new: Call operator new function to get dynamic memory, and then to call the constuctor function.
  • 使用新:调用操作符新函数动态内存,然后调用constuctor函数。
  • 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:不调用操作符新函数,直接调用constuctor函数。栈将被直接使用,不用于malloc。

#9


-2  

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;
}

#1


107  

The line:

线:

Time t (12, 0, 0);

... allocates a variable of type Time in local scope, generally on the stack, which will be destroyed when its scope ends.

…在局部范围内分配类型时间的变量,通常在堆栈上,在其范围结束时将被销毁。

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 generally done on the heap (by default) and requires that you delete it later in the program, while the pointer in t is generally stored on the stack.

…分配一块内存通过调用::运营商新的()或时间::运营商新(),并随后调用时间:时间()这组内的一个地址,内存块(也作为结果返回新的),然后存储在t。如你所知,这通常是在堆(默认情况下),并要求您删除它在之后的计划,而在t通常存储在堆栈的指针。

#2


24  

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();

#3


5  

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.

就构造函数而言,这两种形式在功能上是完全相同的:它们只会导致在新分配的对象实例上调用构造函数。您似乎已经很好地理解了分配模式和对象生存期之间的差异。

#4


3  

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是否为指针而不同。

#5


1  

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));

#6


1  

No.. There is no other difference..

没有. .没有其他的区别。

#7


1  

There is no other difference to what you know already.

你所知道的没有其他的差别。

Assuming your code is using the service of default operator new.

假设您的代码正在使用默认操作符new的服务。

#8


1  

  • Use new: Call operator new function to get dynamic memory, and then to call the constuctor function.
  • 使用新:调用操作符新函数动态内存,然后调用constuctor函数。
  • 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:不调用操作符新函数,直接调用constuctor函数。栈将被直接使用,不用于malloc。

#9


-2  

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;
}