C ++中的新运算符和java中的新运算符之间的差异 [英] Diffrence between new operator in C++ and new operator in java

查看:119
本文介绍了C ++中的新运算符和java中的新运算符之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知, new 运算符执行以下操作:(如果我错了,请更正我。)


  1. 分配内存,然后返回
    分配的内存的第一个块的引用。

  2. 初始化对象(调用构造函数。)

同样,运算符 new [] 以类似的方式工作,除了它为数组中的每个元素。



任何人都可以告诉我这些操作符和C ++和Java中的不同:


  1. 在其生命周期方面。

  2. 如果他们无法分配内存会怎么样。


解决方案


  • 在C ++中, T * p = new T; ...


    1. T $ <$> <$>

      c $ c>,可能会初始化它,并且


    2. 返回指向该对象的指针。 (该指针与标准 new 的已分配内存的地址具有相同的值,但对于数组形式不必如此, )


    如果内存分配失败, std :: bad_alloc 被抛出,没有对象被构造,没有分配内存。



    抛出异常,没有对象被(明显)构造,内存会立即自动释放,并传播异常。



    否则会动态分配对象已被构造,并且用户必须手动销毁对象并释放内存,通常通过说 delete p;



    实际的分配和释放功能可以在C ++中进行控制。如果没有别的,使用全局的,预定义的函数 :: operator new(),但这可以由用户替换;并且如果存在静态成员函数 T :: operator new ,则将使用该成员函数。


  • p>在Java中,它非常相似,只是 new 的返回值可以绑定到 T (或其基础,例如 Object ),并且您必须总是有一个初始化器(所以你会说 T x = new T(); )。对象的生命周期是不确定的,但保证至少只要任何变量仍然引用对象,并且没有办法(也不需要)手动销毁对象。 Java没有明确的内存概念,你不能控制内存的分配。




不同形式的 new 表达式(所谓的展示位置表单)。它们都创建必须手动销毁的动态存储对象,但它们可以是相当任意的。据我所知,Java没有这样的设施。






最大的区别可能在于在Java中,你一直使用 new ,并且,因为它是唯一的创建方式(类类型)对象。相比之下,在C ++中,用户代码中几乎不会有裸的 new 。 C ++有无约束的变量,因此变量本身可以是对象,这就是对象在C ++中通常使用的方式。


As far as I know, the new operator does the following things: (please correct me if I am wrong.)

  1. Allocates memory, and then returns the reference of the first block of the allocated memory. (The memory is allocated from heap, obviously.)
  2. Initialize the object (calling constructor.)

Also the operator new[] works in similar fashion except it does this for each and every element in the array.

Can anybody tell me how both of these operators and different in C++ and Java:

  1. In terms of their life cycle.
  2. What if they fail to allocate memory.

解决方案

  • In C++, T * p = new T;...

    1. allocates enough memory for an object of type T,

    2. constructs an object of type T in that memory, possibly initializing it, and

    3. returns a pointer to the object. (The pointer has the same value as the address of the allocated memory for the standard new, but this needn't be the case for the array form new[].)

    In case the memory allocation fails, an exception of type std::bad_alloc is thrown, no object is constructed and no memory is allocated.

    In case the object constructor throws an exception, no object is (obviously) constructed, the memory is automatically released immediately, and the exception is propagated.

    Otherwise an dynamically allocated object has been constructed, and the user must manually destroy the object and release the memory, typically by saying delete p;.

    The actual allocation and deallocation function can be controlled in C++. If there is nothing else, a global, predefined function ::operator new() is used, but this may be replaced by the user; and if there exists a static member functionT::operator new, that one will be used instead.

  • In Java it's fairly similar, only that the return value of new is something that can bind to a Java variable of type T (or a base thereof, such as Object), and you must always have an initializer (so you'd say T x = new T();). The object's lifetime is indeterminate, but guaranteed to be at least as long as any variables still refer to the object, and there is no way to (nor any need to) destroy the object manually. Java has no explicit notion of memory, and you cannot control the interna of the allocation.

Furthermore, C++ allows lots of different forms of new expressions (so-called placement forms). They all create dynamic-storage objects which must be destroyed manually, but they can be fairly arbitrary. To my knowledge Java has no such facilities.


The biggest difference is probably in use: In Java, you use new all the time for everything, and you have to, since it's the one and only way to create (class-type) objects. By contrast, in C++ you should almost never have naked news in user code. C++ has unconstrained variables, and so variables themselves can be objects, and that is how objects are usually used in C++.

这篇关于C ++中的新运算符和java中的新运算符之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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