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

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

问题描述

我了解硬件会限制程序执行期间分配的内存量.但是,我的问题是与硬件无关.假设对内存量没有限制,那么对阵列也没有限制吗?

解决方案

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

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

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

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

例如,一个一致的实现可以有等于2 1024 -1,这意味着它可以原则上有对象高达179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137215个字节.

祝您好运,发现实际上支持此类对象的硬件.

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

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?

解决方案

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

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.)

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).

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.

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.

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天全站免登陆