英文:
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 |
英文:
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 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 |
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论