在 c++ 中声明大字符数组

发布于 2025-01-08 14:48:00 字数 466 浏览 0 评论 0原文

我现在正在尝试声明一个大字符数组。我使用字符数组作为位图(如布尔值映射,而不是图像文件类型)。以下代码会生成编译错误。

//This is code before main. I want these as globals.
unsigned const long bitmap_size = (ULONG_MAX/(sizeof(char)));
char bitmap[bitmap_size];

错误是数组维度溢出。我认识到我正在尝试让我的流程消耗大量数据,并且可能存在一些限制阻止我这样做。我很好奇我是否犯了语法错误或者我是否需要从内核请求更多资源。另外,我对用某些类创建位图没有兴趣。谢谢您的宝贵时间。

编辑 ULONG_MAX 很大程度上取决于您所使用的机器。在我正在编译的特定机器上,它的代码量远远超过 42 亿。总而言之,我不会像常量一样使用该常量,至少为了内存分配的目的。

I am trying right now to declare a large character array. I am using the character array as a bitmap (as in a map of booleans, not the image file type). The following code generates a compilation error.

//This is code before main. I want these as globals.
unsigned const long bitmap_size = (ULONG_MAX/(sizeof(char)));
char bitmap[bitmap_size];

The error is overflow in array dimension. I recognize that I'm trying to have my process consume a lot of data and that there might be some limit in place that prevents me from doing so. I am curious as to whether I am making a syntax error or if I need to request more resources from the kernel. Also, I have no interest in creating a bitmap with some class. Thank you for your time.

EDIT
ULONG_MAX is very much dependent upon the machine that you are using. On the particular machine I was compiling my code on it was well over 4.2 billion. All in all, I wouldn't to use that constant like a constant, at least for the purpose of memory allocation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云裳 2025-01-15 14:48:00

ULONG_MAX/sizeof(char)ULONG_MAX 相同,是一个非常大的数字。事实上,它太大了,甚至在虚拟内存中也没有足够的空间(因为 ULONG_MAX 可能是整个虚拟内存中的字节数)。

你绝对需要重新考虑你想要做什么。

ULONG_MAX/sizeof(char) is the same as ULONG_MAX, which is a very large number. So large, in fact, that you don't have room for it even in virtual memory (because ULONG_MAX is probably the number of bytes in your entire virtual memory).

You definitely need to rethink what you are trying to do.

月隐月明月朦胧 2025-01-15 14:48:00

在大多数系统上声明这么大的数组是不可能的——在 32 位系统上,该数组为 4 GB,这不适合可用的地址空间,而在大多数 64 位系统上,它为 16 艾字节 (16百万 TB),这也不适合那里的可用地址空间(顺便说一句,可能比整个地球上存在的内存还要多)。

使用malloc() 分配大量内存。但要现实一点。 :)

It's impossible to declare an array that large on most systems -- on a 32-bit system, that array is 4 GB, which doesn't fit into the available address space, and on most 64-bit systems, it's 16 exabytes (16 million terabytes), which doesn't fit into the available address space there either (and, incidentally, may be more memory than exists on the entire planet).

Use malloc() to allocate large amounts of memory. But be realistic. :)

把昨日还给我 2025-01-15 14:48:00

据我了解,c++中数组的最大大小是平台支持的最大整数。您的长类型 bitmap_size 常量可能超出了该限制。

As I understand it, the maximum size of an array in c++ is the largest integer the platform supports. It is likely that your long-type bitmap_size constant exceeds that limit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文