【问题标题】:Java: Size limitation on direct IntBuffer?Java:直接 IntBuffer 的大小限制?
【发布时间】:2015-10-07 11:03:03
【问题描述】:

我想在 Java 中分配一个 direct IntBuffer,例如十亿个元素(64 位系统)。我知道的唯一方法是创建直接 ByteBuffer 并将其视为直接 IntBuffer。但是,4*1,000,000,000 超过了 Integer.MAX_VALUE,所以我的问题是:如何才能实现我的目标?

int numInts = 1_000_000_000;
IntBuffer i = IntBuffer.allocate(numInts); // works!

ByteBuffer bb = ByteBuffer.allocateDirect(4*numInts); // does NOT work: integer overflow
IntBuffer ib = bb.asIntBuffer();
System.out.println("This will never be printed");

非常感谢,

马库斯

【问题讨论】:

  • 如何创建一个二维缓冲区数组?
  • 这不是 IntBuffer 的限制,而是 java 的限制。 java中的数组长度不能大于2^31。
  • 你是对的,但是我的缓冲区大小不需要超过 2^31 个整数,问题是由于(必要的?)通过 ByteBuffer 绕道而出现的。

标签: java bytebuffer addressing


【解决方案1】:

也许您可以尝试创建一个基于 BigInteger 的缓冲区。有些人在 Github 上创建了一个可以启发你的“BigIntbuffer”类: See here

【讨论】:

    猜你喜欢
    • 2014-09-07
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多