C ++中的new运算符与java中的new运算符之间的区别 [英] Difference between new operator in C++ and new operator in java

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

问题描述

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

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


  1. 分配内存,然后返回
    分配内存的第一个块的引用。 (很明显,内存是从堆中分配的。)

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

运算符 new [] 也以类似的方式工作,除了它对数组中的每个元素执行此操作。

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

任何人都可以告诉我这两个运算符在C ++和Java中是如何区别的:

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


  1. 就其生命周期而言。

  2. 如果他们无法分配内存怎么办?


推荐答案


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


    1. 为对象分配足够的内存类型 T

    构造类型 T 在该内存中,可能正在初始化它,

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

    返回指向该对象的指针。 (指针与标准 new 的已分配内存地址具有相同的值,但不一定是数组形式的情况new [] 。)

    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[].)

    如果内存分配失败,则会出现类型异常抛出 std :: bad_alloc ,没有构造对象且没有分配内存。

    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.

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

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

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

    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.

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

    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.

    此外,C ++允许批量不同形式的表达式(所谓的展示位置表单)。它们都创建动态存储对象,必须手动销毁,但它们可能相当随意。据我所知,Java没有这样的设施。

    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.

    最大的区别可能是 use :在Java中,您始终使用 new ,而拥有,因为它是唯一的创建方式(类型) )对象。相比之下,在C ++中,你几乎不应该在用户代码中使用 new 。 C ++有无约束的变量,因此变量本身可以是对象,这就是C ++中通常使用对象的方式。

    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 ++中的new运算符与java中的new运算符之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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