编译错误:无法分配一个常量大小为0的数组。为什么我得到这个?

时间:2022-06-07 18:52:24

I faced one issue while working on a C code with Microsoft Visual Studio-2005 compiler.

我在使用Microsoft Visual Studio-2005编译器处理C代码时遇到了一个问题。

I tried to declare a big buffer statically as :

我试图静态地声明一个大缓冲区:

int gbl_data[4096*4096*256]; 

EDIT: This declaration was a global variable in a header file.

编辑:此声明是头文件中的全局变量。

It was giving an compilation error saying – “cannot allocate an array of constant size 0”.

它给出了一个编译错误说 - “不能分配一个常量大小为0的数组”。

Means somehow the size of 4096X4096X256 was getting too large than the MAX_INT_LIMIT size (2^31) and may be wrapping around and become –ve or so. But then it should have given error as “negative subscript”.

意味着4096X4096X256的大小比MAX_INT_LIMIT大小(2 ^ 31)过大,可能会缠绕并变成-ve左右。但是它应该把错误称为“负下标”。

I tried casting the constants as 4096UL x 4096UL x 256UL , still same compilation error.

我尝试将常量转换为4096UL x 4096UL x 256UL,仍然是相同的编译错误。

What is the cause of this error?

这个错误的原因是什么?

Is it because the physical memory size falling short to allocate this large size buffer or what?

是因为物理内存大小不足以分配这个大尺寸缓冲区还是什么?

What is the fix for it?

有什么办法解决的?

Thanks you.

-GM.

2 个解决方案

#1


The array size is not an int, it's an unsigned int. An unsigned int has a max value of 4294967295. You got one more, and so it wraps around to 0.

数组大小不是int,它是unsigned int。 unsigned int的最大值为4294967295.你还有一个,所以它回绕到0。

Casting the constants as longs doesn't change anything, because longs are also 32-bit integers on most platforms.

将常量转换为long并不会改变任何东西,因为longs在大多数平台上也是32位整数。

You could try with long longs instead, but now we run into another little problem.

您可以尝试使用long longs,但现在我们遇到了另一个小问题。

You're trying to allocate 4 billion integers. a 32-bit CPU has a memory space of 4 billion bytes. You're trying to allocate 4 times the maximum theoretical amount of memory that can exist. (16GB)

你正在尝试分配40亿个整数。 32位CPU的内存空间为40亿字节。您试图分配可存在的最大理论内存量的4倍。 (16 GB)

So back to the drawing board. Figure out why you were trying to do this, and what you can do instead.

所以回到绘图板。弄清楚你为什么要这样做,以及你可以做什么。

#2


You're attempting to statically allocate an array of 2^32 (or 4 times the address space, on a 32bit system). The compiler appears to be truncating 4096 * 4096 * 256 (Which is, off the top of my head, 0x10000) to a 32 bit value.

您试图静态分配2 ^ 32(或32位系统上的地址空间的4倍)的数组。编译器似乎将4096 * 4096 * 256(也就是我的头顶,0x10000)截断为32位值。

Depending on your platform, an unsigned long might also be 32bit, and also truncate.

根据您的平台,无符号长度也可能是32位,也可以截断。

I'd suggest you make sure you're compiling for a 64 bit platform (if that's what you're intending), or change the algorithm to either dynamically allocate memory (obviously no more than the address space), or wok with files on disk.

我建议你确保你正在为64位平台编译(如果那是你想要的),或者将算法改为动态分配内存(显然不超过地址空间),或者用文件写入磁盘。

#1


The array size is not an int, it's an unsigned int. An unsigned int has a max value of 4294967295. You got one more, and so it wraps around to 0.

数组大小不是int,它是unsigned int。 unsigned int的最大值为4294967295.你还有一个,所以它回绕到0。

Casting the constants as longs doesn't change anything, because longs are also 32-bit integers on most platforms.

将常量转换为long并不会改变任何东西,因为longs在大多数平台上也是32位整数。

You could try with long longs instead, but now we run into another little problem.

您可以尝试使用long longs,但现在我们遇到了另一个小问题。

You're trying to allocate 4 billion integers. a 32-bit CPU has a memory space of 4 billion bytes. You're trying to allocate 4 times the maximum theoretical amount of memory that can exist. (16GB)

你正在尝试分配40亿个整数。 32位CPU的内存空间为40亿字节。您试图分配可存在的最大理论内存量的4倍。 (16 GB)

So back to the drawing board. Figure out why you were trying to do this, and what you can do instead.

所以回到绘图板。弄清楚你为什么要这样做,以及你可以做什么。

#2


You're attempting to statically allocate an array of 2^32 (or 4 times the address space, on a 32bit system). The compiler appears to be truncating 4096 * 4096 * 256 (Which is, off the top of my head, 0x10000) to a 32 bit value.

您试图静态分配2 ^ 32(或32位系统上的地址空间的4倍)的数组。编译器似乎将4096 * 4096 * 256(也就是我的头顶,0x10000)截断为32位值。

Depending on your platform, an unsigned long might also be 32bit, and also truncate.

根据您的平台,无符号长度也可能是32位,也可以截断。

I'd suggest you make sure you're compiling for a 64 bit platform (if that's what you're intending), or change the algorithm to either dynamically allocate memory (obviously no more than the address space), or wok with files on disk.

我建议你确保你正在为64位平台编译(如果那是你想要的),或者将算法改为动态分配内存(显然不超过地址空间),或者用文件写入磁盘。