正在尝试获取java数组的第一个元素的地址

正在尝试获取java数组的第一个元素的地址,java,unsafe,Java,Unsafe,我确信有人知道如何使用不安全类获取数组的地址。我看过相关的帖子,实际上没有答案 static Unsafe _u; static int Atoi8(char[] s, int i0, int cb) { int n = 0; // loop to process the valid number chars. int baseOffset = _u.arrayBaseOffset(s.getClass()); <-- returns 16 oops!

我确信有人知道如何使用不安全类获取数组的地址。我看过相关的帖子,实际上没有答案

static Unsafe _u;  
static int Atoi8(char[] s, int i0, int cb)  {
    int n = 0;
    // loop to process the valid number chars.
    int baseOffset = _u.arrayBaseOffset(s.getClass());  <-- returns 16 oops!
    for (int i=i0 ; i<i0+cb ; ++i) {
        char ch = _u.getChar(baseOffset + i);
        n = n*10 + ((ch <= '0') ? 0 : ch - '0');
    }
    return n;
}   
static Unsafe\u;
静态intatoi8(char[]s,inti0,intcb){
int n=0;
//循环以处理有效的数字字符。

int baseOffset=\u.arrayBaseOffset(s.getClass());根据JVM的GC,我不确定您所要求的是否真的可能,它有一种在您背后移动东西的倾向。即使您获得了地址,也很难保证它在任何时间长度内的有效性。不安全。getChar获取地址。如何获取char[]中第一个元素的地址s?@azure是的,我知道这一点。如果这在测试过程中崩溃几次,我不在乎。如果测试成功,我有办法处理。当前代码运行时会发生什么情况?另外,您是否尝试过使用
#arrayindexcale
?从
#arrayBaseOffset
文档:If#arrayindexcale返回非零val对于同一类,您可以使用该比例因子以及该基本偏移量来形成新的偏移量,以访问给定类的数组元素。