开发者

what is placement new operator in C++ and does java has it?

开发者 https://www.devze.com 2023-04-05 05:20 出处:网络
I heard about placement new operator of C++. I am confused what it is. However, I can see where it can be used under a question in stackoverflow. I am also confused whether we have this in java or not

I heard about placement new operator of C++. I am confused what it is. However, I can see where it can be used under a question in stackoverflow. I am also confused whether we have this in java or not. So my question is very precise: What is placement new operator and do we have something like it in java?

Note please, don't be confused with other questions on stackove开发者_StackOverflowrflow: they are not duplicate of this question.


The following article explains the meaning of placement new in C++: http://www.glenmccl.com/nd_cmp.htm

This term itself is relevant for overloaded new statement. Since Java does not allow to overload operators at all and specifically new operator the placement new is irrelevant for Java.

But you have several alternatives.

  1. Using factory or builder pattern
  2. Using wrapper/decorator pattern (probably together with factory) that allows changin some class functionality by wrapping its methods.
  3. Aspect oriented programming. It works almost like decorator pattern but can be implemented using byte code modifiction.
  4. Class loader interception


The term "placement new" itself is somewhat ambiguous. The term is used in two different ways in the C++ standard, and thus by the C++ community.

The first meaning refers to any overloaded operator new function which has more than one parameter. The additional parameters can be used for just about anything—there are two examples in the standard itself: operator new(size_t, void*) and operator new(size_t, std::nothrow_t const&).

The second meaning refers to the specific overload operator new(size_t, void*), which is used in fact to explicitly call the constructor of an object on memory obtained from elsewhere: to separate allocation from initialization. (It will be used in classes like std::vector, for example, where capacity() may be greater than size().)

In Java, memory management is integrated into the language, and is not part of the library, so there can be no equivalents.


Placement new allows to specify custom allocators that take extra parameters.

There is also a predefined placement allocator that takes as extra parameter a pointer and that just returns as result of allocation that pointer, basically allowing your code to create objects at the address you specify.

You can however define other types of allocators that take other parameters, for example our debug allocator takes as extra parameters the filename and the line on which the allocation is performed. Storing this extra information with the allocated object allows us to track back to the source code where has been created a certain object instance that for example got leaked or overwritten or used after deallocation.

AFAIK Java works at an higher conceptual level and has no pointer concept (only the null pointer exception ;-) ). Memory is just a black magic box and the programmer never use the idea of memory address.

I only knew Java 1.1 and back then decided to not invest time on that commercial product so may be the logical level of Java lowered enough today to reach the random access memory concept.

0

精彩评论

暂无评论...
验证码 换一张
取 消