2

I've just wrote some code, which accesses the memory. I checked the address (in code) with CheatEngine, and I printed it with System.out, and it's different. I know it's a long value, but in hex, the value 2 is 00000002 (which was the result in CheatEngine), and I get 844287491178496 with java. Why is that? How can I convert the value from the address to an int? And more important, what is the long value I've just read from the memory with java? Here's my code:

import java.lang.reflect.Field; import sun.misc.Unsafe; public class Memory2 { public static void main(String args[]){ Unsafe unsafe = getUnsafe(); long address = 0x002005C; unsafe.getAddress(address); System.out.println(unsafe.getAddress(address)); } public static Unsafe getUnsafe() { try { Field f = Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); return (Unsafe)f.get(null); } catch (Exception e) {} return null; } } 
4
  • 1
    Doesn't Java sandbox this stuff, so it decides where stuff goes, rather than letting you write wherever you want? It seems this was one of it's features, to avoid the hell on earth that came from dealing with this sort of stuff. Commented Dec 4, 2012 at 19:17
  • 1
    @user1161318 Normally yes, but this is the Unsafe class, which goes around all this as its name suggests. Commented Dec 4, 2012 at 19:19
  • I suspect this could be behind it: Fetches a native pointer from a given memory address. If the address is zero, or does not point into a block obtained from #allocateMemory , the results are undefined. Commented Dec 4, 2012 at 19:20
  • I'm not sure I understood this thing. I checked the adress with cheatengine, so it must be in use, it's value is 2, and with java I read a dword from it what I'm not sure but I think it's a length of an int, and anyways if CE says in this adress the value is 2, why does java say it's something else? Commented Dec 4, 2012 at 19:29

2 Answers 2

3

A long in java is 8 bytes, not a dword; your value from CheatEngine appears to be four bytes. I think the difference might be just that. 0000000200000000 hex is 8589934592; the bytes beyond the four you are checking with CheatEngine might explain the value Java is showing.

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

1 Comment

so true, this must be the answer
1

From the source for sun.misc.Unsafe

/** * Fetches a native pointer from a given memory address. If the address is * zero, or does not point into a block obtained from {@link * #allocateMemory}, the results are undefined. * * <p> If the native pointer is less than 64 bits wide, it is extended as * an unsigned number to a Java long. The pointer may be indexed by any * given byte offset, simply by adding that offset (as a simple integer) to * the long representing the pointer. The number of bytes actually read * from the target address maybe determined by consulting {@link * #addressSize}. * * @see #allocateMemory */ public native long getAddress(long address); 

In short it will read 32-bit (as an unsigned value) on a 32-bit JVM and 64-bits on a 64-bit JVM.

Note: Most 64-bit JVMs will use 32-bit references for typical heap sizes.

1 Comment

ok I'm going to look after these values, maybe CE is a 32 bit version, I have 64bit OS

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.