2

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 stackoverflow: they are not duplicate of this question.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
amod
  • 4,190
  • 10
  • 49
  • 75
  • 1
    related http://stackoverflow.com/questions/222557/cs-placement-new – Scorpion Sep 14 '11 at 06:03
  • @Scoprpion... It might be related but is not same please dont flag it. – amod Sep 14 '11 at 06:06
  • 1
    @amod0017: it **exactly** answers 1 part of your question, leaving only the "Does Java have it" part to be answered. You should edit your question and link to this question. – Joachim Sauer Sep 14 '11 at 08:21

3 Answers3

4

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
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 1
    C++ placement allocators works at the **raw memory** level. What you provide as "alternatives" are constructions at a much higher logical level. Like you said placement allocators are "irrilevant" in Java because in Java there is no raw memory concept. – 6502 Sep 14 '11 at 07:02
  • @AlexR- in placement new operator we fix the position in the memory in the newly created object. Can provide me any way by which i can do that??? – amod Sep 14 '11 at 08:20
  • 2
    OK, @amod0017, the lowest level of memory management existing in Java is class sun.misc.Unsafe. Try to read about it. Probably it may help you. It is almost like malloc in C – AlexR Sep 14 '11 at 13:33
3

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.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
-1

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.

6502
  • 112,025
  • 15
  • 165
  • 265
  • 1
    What rant sorry? I didn't invest time on **that** specific commercial product but I invested time in other commercial products. A language is a language, a product is a product. Java is not a language and recent acquisition by Oracle and patent litigation made this issue 100% clear... what part of this reasoning you think is a "rant"? I'm just saying how things are, if you don't like them it's not my fault. – 6502 Sep 14 '11 at 06:18
  • 1
    What definition of "programming language" would exclude Java? The comment adds no value to the answer; the only reason it's there is to rile up people. Thus, it's a rant. – NullUserException Sep 14 '11 at 06:21
  • @NullUserException: A language is a specification not in control of a single company. I got myself burnt many years ago when I was in love with TurboPascal, thinking to it as a language. But it wasn't and at the finger snap of a marketdroid it disappeared to be replaced with another product completely different (Delphi). When you use a language someone else cannot decide to destroy all you know and you built. C is a language, LISP is a language, Java is a product, Microsoft Access is a product, C# is a product. Seeing your website is oracle.com I can understand your point of view is different. – 6502 Sep 14 '11 at 06:32
  • I agree the parenthesized expression was unnecessary tho. Fixed. – 6502 Sep 14 '11 at 06:34
  • 8
    @6502: Why does a "language" have to not be in the control of a company to be a "language"? I can understand you not _liking_ a language that's not more open. But saying that a language under the control of a company isn't a "language" is like saying that software isn't "software" unless it's open-source. It's trying to appropriate a word to mean what you want it to mean rather than the accepted definition. – Nicol Bolas Sep 14 '11 at 06:37
  • @Nicol Bolas: IMO a "language" is program/product/company agnostic. I'm **not** an open source fanatic... actually the reason I didn't pick up Java was not because Java was closed, but because it was dead ugly. But for example at that time I loved PowerBuilder (a **product** from Powersoft). As an additional point (but it's an extra) I prefer if possible building things knowing that no one can claim my bricks back. Any software that includes a programming capability is for you a language? Feel free to call Microsoft Access a language... I think it's a product. – 6502 Sep 14 '11 at 06:53
  • 3
    You are of course free to make up your own definitions for words. But in doing that, aren't you defeating the purpose of language (natural language)? Are there other people who claim Java isn't a language? I mean, nothing stops me from saying dogs are plants. It's just that most people will think I am stupid. – NullUserException Sep 14 '11 at 07:10
  • 1
    Languages are not software. That Microsoft Access example is ill-chosen. (Nothing prevents Java from both being a language and some of its implementations being products.) – Luc Danton Sep 14 '11 at 08:42
  • @NullUserException: In my opinion a language is not an implementation of a language. Java is a huge implementation and the "language" is defined as "whatever oracle decides it to be". So it's not me playing Humpty Dumpty, it's Sun first and Oracle later that went down this path. Java(tm) means just that, i.e. nothing except what Oracle wants it to mean at a given time. I see a clear distinction between say C++/Fortran and Java(tm)/MS Access(tm). You're of course free not not see the difference and to call me stupid. – 6502 Sep 14 '11 at 11:11
  • So what's the difference between Java being what "Oracle decides what it's to be" and C++ being what it is because the "C++ committee decided what it was to be"? – NullUserException Sep 14 '11 at 18:16
  • @NullUserException: Many differences in quantity, quality, guidelines. The guidelines part is extremely important, for example oracle will decide for what is the best for oracle (and that will be different from the best for java users, erm, customers). Also I don't think the C++ committee is going to sue google for invalid uses of C++. By the way I've to thank you for some good reading you made me doing about JCP ... I don't really follow Java (I find it ugly) but it's important to keep an eye out on how things are going in general and Java (unfortunately) is still a big part of IT. – 6502 Sep 14 '11 at 21:17
  • A bit late, but... if we're being technical, Oracle sued Google for infringing the Java APIs, not for using Java in and of itself; it was basically a plagiarism lawsuit. An apt comparison would be C/C++ vs. the Windows CRT, .NET Framework, or DirectX; Microsoft isn't going to sue you if you write a C/C++ program for Windows, but they might if you copied part of the CRT's or .NET's source code directly into your program. (Microsoft seems to be more lenient, surprisingly; their answer to .NET being unofficially ported to Linux was to open-source .NET & make their own Linux version, not sue.) – Justin Time - Reinstate Monica May 10 '16 at 18:04
  • Alternatively, it would be like if the C++ committee tried to patent the GCC and/or MS implementation of the Standard Library, and sue anyone who makes another implementation. Considering that part of the lawsuit appears to be over some of the Android Java API's methods having the same signature as methods in the Oracle API, it appears they were trying to _literally_ do exactly that. If they were allowed to do so, of course, it would both cripple Android Java and guarantee that any program written for Android would be incompatible with the JRT (and vice versa), which was likely their intent. – Justin Time - Reinstate Monica May 10 '16 at 18:15
  • The issue stems from Java (the language) ostensibly being intended for platform-agnostic development, but being _very_ tightly coupled to the Java Runtime (the product). While this _is_ somewhat necessary considering the nature of the language, there's nothing that forces it to be coupled specifically to the _Oracle_ JRT. They were likely attempting to guarantee that code was only cross-platform if written for _their_ implementation, to force other companies to specifially use the Oracle JRT instead of their own. – Justin Time - Reinstate Monica May 10 '16 at 18:17
  • While this would eliminate implementation-specific JRT quirks, it would also give Oracle a monopoly on both Java implementations _and_ compilers/interpreters. In this regard, it would be similar to if MS were to sue anyone that: made a Windows-compatible compiler, made a program that let you run code one line at a time on Windows, or made an implementation of Windows-specific libraries for other OSes and/or statically linked the CRT into a program for other OSes. – Justin Time - Reinstate Monica May 10 '16 at 18:21