简体   繁体   English

使用c数组时堆栈溢出

[英]Stack overflow when using c array

When I define 当我定义

void tfooo(){
int arr[SOME_LARGE_NUMBER];
// some code here
}

I am getting stack overflow, but when I add the static keyword 我正在获得堆栈溢出,但是当我添加static关键字时

void tfooo(){
static int arr[SOME_LARGE_NUMBER];
// some code here
}

everything is fine. 一切都好。

What is the difference? 有什么不同? Isn't static arrays as opposed to dynamic arrays always defined on the stack? 是不是静态数组而不是总是在堆栈上定义的动态数组?

It is often the case that an object declared automatic is allocated on the stack (which is relatively small), whereas an object declared static is allocated elsewhere. 通常情况下,声明为自动的对象在堆栈上分配(相对较小),而声明为static的对象则在其他地方分配。

Notice, this depends on your machine and on your compiler. 请注意,这取决于您的机器和编译器。

I am generating the assembler code for the static version and non static version, and the only difference is that into the static version the variable does not exist. 我正在为静态版本和非静态版本生成汇编代码,唯一的区别是在静态版本中变量不存在。 I think it has been removed because it is not used into my test. 我认为它已被删除,因为它没有用于我的测试。

Are you using the variable into the test are you doing? 你在使用变量进入测试吗? Maybe the optimiser has removed the variable into the static version. 也许优化器已将变量移除到静态版本中。

I am using gcc. 我正在使用gcc。 To build the assembler code, pass the -S argument: 要构建汇编代码,请传递-S参数:

gcc -S main.c

And this is the test code used for the non static version: 这是用于非静态版本的测试代码:

#define SOME_LARGE_NUMBER (100000000000000000)

int arr[SOME_LARGE_NUMBER];

int main(const int argc, const char* argv[]) {
    return 0;
}

And this for the static version: 这对于静态版本:

#define SOME_LARGE_NUMBER (100000000000000000)

static int arr[SOME_LARGE_NUMBER];

int main(const int argc, const char* argv[]) {
    return 0;
}

This is the difference I got: 这是我得到的差异:

$ diff main.static.s main.nostatic.s 
23a24
>   .comm   _arr,400000000000000000,4 ## @arr

For the information you are providing this is what I can get. 对于您提供的信息,这是我能得到的。 Could you paste more details about your code? 你能粘贴更多关于代码的细节吗?


EDIT : Into the image attached we can see the memory layout for a windows application. 编辑 :在附加的图像中,我们可以看到Windows应用程序的内存布局。 When we use the static modifier into a function, it is stored into the .data segment instead of the stack of the program, because of that you don't get the stack overflow. 当我们将static修饰符用于函数时,它被存储到.data段而不是程序的堆栈中,因为你没有得到堆栈溢出。 In compiling time, the size of the array is known, so that, the binary image store enough space for the data. 在编译时,数组的大小是已知的,因此二进制图像存储了足够的数据空间。 What is the size of your EXE file in both versions? 两个版本中EXE文件的大小是多少? If I am not mistake the EXE file size for the static version will be much bigger than the no static version. 如果我没有错误,静态版本的EXE文件大小将比无静态版本大得多。 I suppose that the size of the array is reserved into the data segment when the binary is loaded. 我想在加载二进制文件时,数组的大小会保留在数据段中。 However, when using the no static version, it depends on how much memory is set up the stack. 但是,在使用无静态版本时,它取决于堆栈设置了多少内存。 You can modify this size using the flag "/F" when compiling from the command line (see this link https://msdn.microsoft.com/en-us/library/tdkhxaks.aspx ). 从命令行编译时,可以使用标志“/ F”修改此大小(请参阅此链接https://msdn.microsoft.com/en-us/library/tdkhxaks.aspx )。 I don't have a VM with windows to double check. 我没有带有Windows的VM来仔细检查。

在此输入图像描述

In overview, your static variable is not stored into the stack, because of that you don't get a stack overflow when using the static version. 概括地说,您的静态变量不会存储到堆栈中,因为在使用静态版本时不会出现堆栈溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM