C中静态和非静态数组的区别。

时间:2022-11-21 00:43:27

I'm wondering why this works up to a size of 1e8. For sizes larger than 1e8, the compiler says "size too large".

我想知道为什么它的大小是1e8。对于大于1e8的大小,编译器会说“size太大”。

#include <stdio.h>
int main() {
    printf("allocating...\n");
    static float m[(int)1e8];
    printf("done\n");
}

While this works only up to 1e5. If size is set to 1e6, it does compile fine, but crashes in runtime even before the first line is printed.

而这只适用于1e5。如果将size设置为1e6,它会编译良好,但是在第一行打印之前就会崩溃。

#include <stdio.h>
int main() {
    printf("allocating...\n");
    float m[(int)1e5];
    printf("done\n");
}

What are these limits? And why the static has a higher limit?

这些限制是什么?为什么静态会有更高的极限?


edit: platform is MinGW64 in windows7. Haven't tested it in linux yet.

编辑:在windows7中,平台是MinGW64。还没有在linux中测试过它。

2 个解决方案

#1


5  

In terms of C, an object with static storage duration lives for the whole execution time of the program, while one with automatic storage duration (which is the default in a function scope) only lives within its scope. The immediate consequence is that you will only ever have one instance of the static version, while an automatic object is created each time you call the function. C doesn't have anything to say about size limits.

在C语言中,一个具有静态存储持续时间的对象是程序的整个执行时间,而一个具有自动存储持续时间(在函数范围内是默认值)的对象只在其范围内生存。直接的结果是,您只会有一个静态版本的实例,而在每次调用该函数时都会创建一个自动对象。C没有任何关于尺寸限制的东西。

But of course, there's a simple explanation: Most implementations of C use the stack to store automatic variables. Every function call gets a stack frame of its own, so this assures every function works with its own instance of a "local variable". Operating systems limit the total size of the stack, and that's the limit you're hitting here. On the other hand, an object with static storage duration is defined directly in your executable, this memory will be taken from the OS upon start of your process.

当然,有一个简单的解释:C的大多数实现都使用堆栈来存储自动变量。每个函数调用都有自己的堆栈框架,因此这确保每个函数都使用“局部变量”的实例。操作系统限制了堆栈的总大小,这就是你在这里要达到的极限。另一方面,在可执行文件中直接定义具有静态存储持续时间的对象,此内存将从进程开始时从操作系统中提取。

#2


2  

Static declarations are usually allocated as soon as the program starts, and are still allocated for as long as your program runs, while non-static declarations are usually stored on the stack, and may be reused as soon as the function terminates. This is probably why these two allocations behave differently.

静态声明通常在程序启动时立即分配,并且在程序运行时仍然会分配,而非静态声明通常存储在堆栈中,并且在函数终止时可能会重新使用。这可能就是这两种分配行为不同的原因。

I'd like to say that in the second case, the fact that your program crashes is linked to how your OS handles the stack. But I can't be sure

我想说的是,在第二种情况下,程序崩溃的事实与你的操作系统处理堆栈的方式有关。但我不能肯定。

#1


5  

In terms of C, an object with static storage duration lives for the whole execution time of the program, while one with automatic storage duration (which is the default in a function scope) only lives within its scope. The immediate consequence is that you will only ever have one instance of the static version, while an automatic object is created each time you call the function. C doesn't have anything to say about size limits.

在C语言中,一个具有静态存储持续时间的对象是程序的整个执行时间,而一个具有自动存储持续时间(在函数范围内是默认值)的对象只在其范围内生存。直接的结果是,您只会有一个静态版本的实例,而在每次调用该函数时都会创建一个自动对象。C没有任何关于尺寸限制的东西。

But of course, there's a simple explanation: Most implementations of C use the stack to store automatic variables. Every function call gets a stack frame of its own, so this assures every function works with its own instance of a "local variable". Operating systems limit the total size of the stack, and that's the limit you're hitting here. On the other hand, an object with static storage duration is defined directly in your executable, this memory will be taken from the OS upon start of your process.

当然,有一个简单的解释:C的大多数实现都使用堆栈来存储自动变量。每个函数调用都有自己的堆栈框架,因此这确保每个函数都使用“局部变量”的实例。操作系统限制了堆栈的总大小,这就是你在这里要达到的极限。另一方面,在可执行文件中直接定义具有静态存储持续时间的对象,此内存将从进程开始时从操作系统中提取。

#2


2  

Static declarations are usually allocated as soon as the program starts, and are still allocated for as long as your program runs, while non-static declarations are usually stored on the stack, and may be reused as soon as the function terminates. This is probably why these two allocations behave differently.

静态声明通常在程序启动时立即分配,并且在程序运行时仍然会分配,而非静态声明通常存储在堆栈中,并且在函数终止时可能会重新使用。这可能就是这两种分配行为不同的原因。

I'd like to say that in the second case, the fact that your program crashes is linked to how your OS handles the stack. But I can't be sure

我想说的是,在第二种情况下,程序崩溃的事实与你的操作系统处理堆栈的方式有关。但我不能肯定。