4

I come from a C++ background. In C++ I can store a memory adress which I just new'd in a global array and re-use it later. For example, say I have two classes X, Y and I create two objects x, y. The global array StoreAddresses[2] is defined as:

 uint32_t StoreAddresses[2]; 

I write:

 X * x = new X(); Y * y = new Y(); StoreAdresses[0] = (uint32t *) x; //for example, 0x12345678 StoreAdresses[1] = (uint32t *) y; //for example, 0x12345698 

Anywhere in my program, I can retrieve the data written in memory by calling:

 X * stored_x = (X*)StoreAdresses[0]; Y * stored_y = (Y*)StoreAdresses[1]; 

How can I accomplish that in Java? Appreciate the help!

5
  • 1
    One of the principle differences between Java and C/C++ is that Java restricts the programmer's ability to use and abuse memory. I suppose that you could use a byte array to simulate this (or use unsafe as Peter Lawrey states -- 1+ to him), but better would be for you to learn the ways of Java and adapt your coding to its strengths, not its weaknesses. Commented Dec 26, 2011 at 12:14
  • agree with @HovercraftFullOfEels - to achieve what you seem to want to do, you should store the references of X and Y in a static field, instead of mucking around with memory addresses. Commented Dec 26, 2011 at 12:19
  • The real question is: why would you want to do this Commented Dec 26, 2011 at 12:21
  • I should bet that what you need is a couple of big byte arrays to do direct byte manipulation. In case you don't need direct byte manipulation don't access memory!! Access you objects! In Java you always manipulate references. So writing X x = new X(); x.doThis() is equivalent to C++ X* x = new X(); x->doThis(). Always think in Object pointers and arrow access. Commented Dec 26, 2011 at 12:26
  • Thank you for posting guys. I'll reply to Peter's post below. I'm already deterred from using Unsafe. Commented Dec 26, 2011 at 12:31

4 Answers 4

7

You can do this in Java using Unsafe. However its a very bad idea. That is because address of an object can change at any time (by a GC) If you store the address like this an use it later it might not be valid any more and even crash your application. If the GC believes an object does not have a strong reference, it may clean up the object. When you try to reference the object again, it may not be in the JVM any more.

The size of an address can change depending on how you start the JVM. A 64-bit JVM usually uses 32-bit references (which might a surprise coming from C++) You can get this with Unsafe.ADDRESS_SIZE

I would only use Unsafe to see what the addresses are to see how the objects are laid out in your cache. (Which is not very useful as there is not much you can about it :( )

It is likely that what ever you are trying to can be done a better way. Can you give us more details?

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Peter. Thank you for the elaborate answer. The reason why I went to such lengths was a simple mistake I made in my code while using inheritance... I have one base class and 4 children classes. Even though I declared my original public object in the base class, I new'ed it by mistake in one of the children. So, the reference was null in all other classes :( This is why I thought of the C++ trick. Now that I defined it in the base class, problem solved! Btw, the C++ memory manipulation works perfectly in my commercial app in different archs w/o flaws. Thanks again for the input!
It works in C++ because your pointers won't be changed while you are using them.
2

What Peter said is correct. I would add that you need to start thinking in java terms, not in C++ terms.

It might help you to know that in Java X x = new X(); is almost entirely equivalent to what in C++ is X* x = new X();

1 Comment

Hi Mike, I do know all these stuff. Please read my comment to Peter :)
1

Ya. You can accomplish this in Java. But ,for manipulating address , C++ is is obviously a better choice. Java does not provide that facility to manipulate an address easily for security reason.

If you call the hashCode() on any object , it will return the string representation of the address of any object .

1 Comment

Thanks Mukherjee. I can also use the toString and read the substring that follows the '@' symbol.
0

In java, you can use object references. There aren't void or integral pointers, but since every object inherits from Object, you can use cast Object references instead

import java.lang.*; class test { static Object[] StoreObjectRefs = new Object[2]; public static void main(String args[]) { Integer x = new Integer(1); Float y = new Float(2.0); StoreObjectRefs[0] = (Object) x; StoreObjectRefs[1] = (Object) y; System.out.println((Integer)StoreObjectRefs[0]+(Float)StoreObjectRefs[1]); } } 

Be careful though, references are passed by value in functions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.