As far as I know, the new
operator does the following things: (please correct me if I am wrong.)
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:
The difference between the two is that operator new just allocates raw memory, nothing else. The new operator starts by using operator new to allocate memory, but then it invokes the constructor for the right type of object, so the result is a real live object created in that memory.
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.
new creates an instance, whereas new[1] creates a single-element array. new[1] will almost certainly incur (small) memory overhead compared to new to store the size of the array.
new operator. The new operator denotes a request for memory allocation on the Free Store. If sufficient memory is available, a new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.
T * p = new T;
...allocates enough memory for an object of type T
,
constructs an object of type T
in that memory, possibly initializing it, and
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 a 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 function T::operator new
, that one will be used instead.
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 new
s in user code. C++ has unconstrained variables, and so variables themselves can be objects, and that is how objects are usually used in C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With