C中数组的最大大小是多少? [英] What is the maximum size of an array in C?

查看:175
本文介绍了C中数组的最大大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解硬件会限制程序执行期间分配的内存量.但是,我的问题是不考虑硬件.假设内存量没有限制,那么数组就没有限制了吗?

I understand that hardware will limit the amount of memory allocated during program execution. However, my question is without regard to hardware. Assuming that there was no limit to the amount of memory, would there be no limit to the array?

推荐答案

C 中的数组大小没有固定限制.

There is no fixed limit to the size of an array in C.

任何单个对象的大小,包括任何数组对象,都受到SIZE_MAX的限制,size_t类型的最大值,这是的结果>sizeof 运算符.(尚不完全清楚 C 标准是否允许大于 SIZE_MAX 字节的对象,但实际上不支持此类对象;请参阅脚注.)因为 SIZE_MAX 由实现决定, 并且不能被任何程序修改,它为任何单个对象强加 SIZE_MAX 字节的上限.(这是一个上限,而不是至少一个上限;实现可能并且通常会施加较小的限制.)

The size of any single object, including of any array object, is limited by SIZE_MAX, the maximum value of type size_t, which is the result of the sizeof operator. (It's not entirely clear whether the C standard permits objects larger than SIZE_MAX bytes, but in practice such objects are not supported; see footnote.) Since SIZE_MAX is determined by the implementation, and cannot be modified by any program, that imposes an upper bound of SIZE_MAX bytes for any single object. (That's an upper bound, not a least upper bound; implementations may, and typically do, impose smaller limits.)

void* 类型的宽度,一个泛型指针类型,对一个正在执行的程序中所有对象的总大小强加了一个上限(可能大于单个对象的最大大小)对象).

The width of the type void*, a generic pointer type, imposes an upper bound on the total size of all objects in an executing program (which may be larger than the maximum size of a single object).

C 标准对这些固定大小强加了下限,但没有强加上限.没有符合标准的 C 实现可以支持无限大小的对象,但它原则上可以支持任何有限大小的对象.上限是由各个 C 实现、它们运行的​​环境以及物理而不是语言强加的.

The C standard imposes lower bounds, but not upper bounds, on these fixed sizes. No conforming C implementation can support infinite-sized objects, but it can in principle support objects of any finite size. Upper bounds are imposed by individual C implementations, by the environments in which they operate, and by physics, not by the language.

例如,一个符合要求的实现可以使 SIZE_MAX 等于 21024-1,这意味着它可以原则上拥有最多179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137215个字节.

For example, a conforming implementation could have SIZE_MAX equal to 21024-1, which means it could in principle have objects up to 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137215 bytes.

祝您找到真正支持此类对象的硬件.

Good luck finding hardware that actually supports such objects.

脚注:没有明确规定任何对象都不能大于 SIZE_MAX 字节.您无法将 sizeof 运算符有效地应用于此类对象,但与任何其他运算符一样,sizeof 可能会溢出;这并不意味着您不能对这样的对象执行操作.但实际上,任何合理的实现都会使 size_t 大到足以表示它支持的任何对象的大小.

Footnote: There is no explicit rule that no object can be bigger than SIZE_MAX bytes. You couldn't usefully apply the sizeof operator to such an object, but like any other operator, sizeof can overflow; that doesn't mean you couldn't perform operations on such an object. But in practice, any sane implementation will make size_t big enough to represent the size of any object it supports.

这篇关于C中数组的最大大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆