为什么我无法在Windows 11上使用C++创建大于8GiB的堆?

huangapple go评论78阅读模式
英文:

Why I can't use C++ to create a heap bigger than 8GiB on Windows 11

问题

I use CLion to build this code on Windows 11. My system is 64bit and has 32GiB memory.

#include <iostream>
#include <chrono>
#include <thread>

int main() {
    int len = 0;
    std::cin >> len;
    auto arg = std::make_unique<int32_t[]>(len);
    std::this_thread::sleep_for(std::chrono::seconds(100));
}

I'm trying to create a large dynamic array. When the type of this array is int (4字节), the maximum size of this array is around 8192MiB. If I change the type of this array to int16_t (2字节), the maximum size of this array is around 4096MiB. If I change the type of this array to char (1字节), the maximum size of this array is around 2048MiB.

| 变量大小 | 最大内存(MiB) |
| ------ | --------------- |
| 4字节  | 8192            |
| 2字节  | 4096            |
| 1字节  | 2048            |

Code

VMMap

TaskManager

英文:

I use CLion to build this code on Windows 11. My system is 64bit and has 32GiB memory.

#include &lt;iostream&gt;
#include &lt;chrono&gt;
#include &lt;thread&gt;

int main() {
    int len = 0;
    std::cin &gt;&gt; len;
    auto arg = std::make_unique&lt;int32_t[]&gt;(len);
    std::this_thread::sleep_for(std::chrono::seconds(100));
}

I'm trying to create a large dynamic array. When type of this array is int (4 BYTES), the maximum size of this array is around 8192MiB. If I change type of this array to int16_t (2 BYTES), the maximum size of this array is around 4096MiB. If I change type of this array to char (1 BYTES), the maximum size of this array is around 2048MiB.

| Size of variables | Max Memory (MiB) |
| ----------------- | ---------------- |
|      4 BYTES      |       8192       |
|      2 BYTES      |       4096       |
|      1 BYTES      |       2048       |

Code

VMMap

TaskManager

答案1

得分: 4

int 可以容纳的典型最大值为 2,147,483,647,看起来您的最大大小受到 int 的这个限制。

尝试使用 long long 代替 int 作为 len 的数据类型。

英文:

The typical maximum value that int can hold is 2,147,483,647 and it looks like your maximum size is limited by this limit of int.

Try using long long instead of int for len.

huangapple
  • 本文由 发表于 2023年3月3日 22:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定