fork(3) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. static final sun.misc.Unsafe unsafe;
  12.  
  13. static
  14. {
  15. try
  16. {
  17. final java.lang.reflect.Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
  18. field.setAccessible(true);
  19. unsafe = (sun.misc.Unsafe) field.get(null);
  20. } catch (final Throwable e)
  21. {
  22. throw new Error("Can't find unsafe instance.", e);
  23. }
  24. }
  25.  
  26. public static void main(String[] args)
  27. {
  28. long memStart = unsafe.allocateMemory(4 * 3);
  29. long ptr1 = memStart + (4 * 1);
  30. long ptr2 = memStart + (4 * 2);
  31.  
  32. System.out.println("Ptr1: " + Long.toHexString(ptr1) + ", Ptr2: " + Long.toHexString(ptr2) + ", Result: " + Long.toHexString(memStart));
  33. unsafe.putInt(ptr1, 5);
  34. unsafe.putInt(ptr2, 15);
  35.  
  36. add(ptr1, ptr2, memStart);
  37. System.out.println(unsafe.getInt(memStart));
  38. }
  39.  
  40. public static void add(long intA, long intB, long result)
  41. {
  42. unsafe.putInt(result, unsafe.getInt(intA) + unsafe.getInt(intB));
  43. }
  44. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
Ptr1: 94a7b74, Ptr2: 94a7b78, Result: 94a7b70
20