为什么C没有为数组定义的最小尺寸? [英] Why does C not define minimum size for an array?

查看:142
本文介绍了为什么C没有为数组定义的最小尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C标准定义了很多的下限/上限( 翻译限制 ),并规定一个实现应满足每一份稿件。为什么有一个数组的大小定义没有这样的最低限额?下面的程序将要编译罚款和可能产生运行时错误/段错误,并会调用的未定义行为

C standard defines a lot of lower/upper limits (translation limits) and imposes an implementation should satisfy for each translation. Why there's no such minimum limit defined for an array size? The following program is going to compile fine and likely produce runtime error/segfault and would invoke undefined behaviour.

int main()
{
   int a[99999999];
   int i;

   for(i=0;i<99999999;i++)
   a[i]=i;

   return 0;
}

一个可能的原因是本地阵列上的自动存储分配,它依赖于分配的堆栈帧的大小。但是,为什么没有最低限额像C中规定的其他限制?

A possible reason could be local arrays are allocated on automatic storage and it depends on the size of the stack frame allocated. But why not a minimum limit like other limits defined by C?

让我们忘掉不确定的情况下,像上面。考虑以下几点:

Let's forget about the undefined cases like above. Consider the following:

int main()
{
   int a[10];
   int i;

   for(i=0;i<10;i++)
   a[i]=i;

   return 0;
}

在上面,给我带来保证本地阵列(尽管非常小的)正在按计划进行工作,不会造成不确定的行为,由于分配失败?

In the above, what gives me the guarantee that the local array (despite a very small one) is going to work as expected and won't cause undefined behaviour due to allocation failure?

虽然这是不可能的,这样一小阵的分配将无法在任何现代系统。但C标准没有定义任何的要求,以满足和编译器不会(至少GCC没有)报告分配失败。只有运行时错误/不确定的行为可能性。的难的是没有人可以告诉一个任意大小的数组是否会导致未定义的行为,由于分配失败。

Although it's unlikely that an allocation for such a small array would fail on any modern systems. But the C standard doesn't define any requirements to satisfy and compilers don't (at least GCC doesn't) report allocation failures. Only a runtime error/undefined behaviour is possibility. The hard part is nobody can tell whether an arbitrary sized array is going cause undefined behaviour due to allocation failure.

请注意,我知道我可以使用动态数组(通过的的malloc 的与朋友们)为此目的,有超过分配失败了更好的控制。我更感兴趣的是为什么有本地数组定义没有这样的限制。此外,全球阵列将要被存储在静态存储器和将要增加的可执行尺寸,其编译器可以处理。

Note that I am aware I can use dynamic arrays (via malloc & friends) for this purpose and have a better control over allocation failures. I am more interested in why there's no such limit defined for local arrays. Also, global arrays are going to be stored in static storage and is going to increase executable size which compilers can handle.

推荐答案

下限为1元素的数组。为什么你将有一个限制是什么?当然,如果你调用一个函数递归永远,1阵列可能不适合堆栈,或者说围绕调用函数下次调用可能不适合堆栈上的呼叫 - 来解决,这将是知道的唯一途径在编译器中堆栈的大小 - 但是编译器实际上并不知道在那个阶段堆栈有多大 - 别提了极其复杂的呼叫层次的问题是几个不同的函数调用到同样的功能,可能与递归和/或你如何分配堆栈的大小为该 - - 最坏的可能情况下,可以不遇到过,因为其他的事情决定了这不会发生 - 而大用户栈几层,例如,在一个函数的最坏的情况是只当输入文件是空的,但在另一个函数的最坏的情况是,当有大量存储在同一文件中的数据。很多很多这样的变化。这太不可靠的判断,所以迟早会刚刚成为猜测的工作或大量误报的。

The MINIMUM limit is an array of 1 element. Why would you have a "limit" for that? Of course, if you call a function recursively forever, an array of 1 may not fit on the stack, or the call that calls the function next call around may not fit on the stack - the only way to solve that would be to know the size of the stack in the compiler - but the compiler doesn't actually know at that stage how big the stack is - never mind the problems of extremely complex call hierarchies were several different functions call into the same function, possibly with recursion and/or several layers of rather large consumers of stack - how do you size the stack for that - the worst possible case may not be ever encountered, because other things dictate that this doesn't happen - for example, the worst case in one function is only when an input file is empty, but the worst case in another function is when there is lots of data stored in the same file. Lots and lots of variations like this. It's just too unreliable to determine, so sooner or later it would just become guess-work or of lots of false positives.

考虑与成千上万的功能,所有这些呼叫,需要在栈上的200字节数组,用于暂时存储日志输出相同的记录功能的程序。它从主要的向上几乎每一个函数调用。

Consider a program with thousands of functions, all of which call the same logging function that needs a 200 byte array on the stack for temporarily storing the log output. It's called from just about every function from main upwards.

对于一个局部变量的最大依赖于堆栈,其中,像我上面说的大小,是不是编译code时,编译器知道[链接器可能知道,但这是以后。对于全局数组和那些在堆中分配,限制为你的过程多少内存得到,所以没有上限那里。

The MAXIMUM for a local variable depends on the size of the stack, which, like I said above, is not something the compiler knows when compiling your code [the linker MAY know, but that's later on]. For global arrays and those allocated on the heap, the limit is "how much memory your process can get", so there's no upper limit there.

只是有没有简单的方法来确定这一点。和许多由标准规定的限值是有保证code可以在任何编译器,只要你的code遵循的规则进行编译。被编译并能够运行到完成是两种不同的事情。

There's just no easy way to determine this. And many of the limits provided by the standard is there to guarantee that code can be compiled on "any compiler" as long as your code follows the rules. Be compiled and be able to run to completion is two different things.

INT的main()
{
    而(1);
}

int main() { while(1); }

绝不会运行到结束 - 但它会在每一个编译器,我知道的编译,大多数都不会说的那里是一个无限循环的事情 - 它是你的选择这样做。

will never run to completion - but it will compile in every compiler I know of, and most won't say a thing about there being an infinite loop - it's your choice to do that.

这也是你的选择将大数组在堆栈中。而且它很可能是在链接程序给定的堆栈的数千兆字节,在这种情况下,它会被罚款 - 或堆栈200K,你不能有50000的整数数组...

It's also your choice to put large arrays on the stack. And it could well be that the linker is given several gigabytes of stack, in which case it'll be fine - or the stack is 200K, and you can't have 50000 array of integers...

这篇关于为什么C没有为数组定义的最小尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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