/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static final sun.misc.Unsafe unsafe;
static
{
try
{
final java.
lang.
reflect.
Field field
= sun.
misc.
Unsafe.
class.
getDeclaredField("theUnsafe"); field.setAccessible(true);
unsafe = (sun.misc.Unsafe) field.get(null);
{
throw new Error("Can't find unsafe instance.", e
); }
}
public static void main
(String[] args
) {
long memStart = unsafe.allocateMemory(4 * 3);
long ptr1 = memStart + (4 * 1);
long ptr2 = memStart + (4 * 2);
System.
out.
println("Ptr1: " + Long.
toHexString(ptr1
) + ", Ptr2: " + Long.
toHexString(ptr2
) + ", Result: " + Long.
toHexString(memStart
)); unsafe.putInt(ptr1, 5);
unsafe.putInt(ptr2, 15);
add(ptr1, ptr2, memStart);
System.
out.
println(unsafe.
getInt(memStart
)); }
public static void add(long intA, long intB, long result)
{
unsafe.putInt(result, unsafe.getInt(intA) + unsafe.getInt(intB));
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCiAgICBzdGF0aWMgZmluYWwgc3VuLm1pc2MuVW5zYWZlIHVuc2FmZTsKCiAgICBzdGF0aWMKICAgIHsKICAgICAgICB0cnkKICAgICAgICB7CiAgICAgICAgICAgIGZpbmFsIGphdmEubGFuZy5yZWZsZWN0LkZpZWxkIGZpZWxkID0gc3VuLm1pc2MuVW5zYWZlLmNsYXNzLmdldERlY2xhcmVkRmllbGQoInRoZVVuc2FmZSIpOwogICAgICAgICAgICBmaWVsZC5zZXRBY2Nlc3NpYmx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