4

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

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?

asked May 28, 2018 at 22:19
10
  • 1
    You might find this helpful: stackoverflow.com/questions/93039/… Commented May 28, 2018 at 22:31
  • @DevSolar Was asking from c perspective. Also I am aware of that both examples are not dynamic allocation but static. The Q is why am I getting stack overflow only in the first example Commented May 28, 2018 at 22:33
  • Possible duplicate of Why declare a variable or function static in C? Commented May 28, 2018 at 22:34
  • 1
    Because the static array -- which outlives the current function invocation -- is allocated in the binary itself (ELF: .data / .bss section). So there is no stack involved. Commented May 28, 2018 at 22:42
  • "as opposed to dynamic arrays"? There are no dynamic arrays in your question. The first array is automatic. And yes, automatic arrays are typically stored on the stack. Commented May 28, 2018 at 22:53

2 Answers 2

2

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.

Notice, this depends on your machine and on your compiler.

answered May 28, 2018 at 22:47
Sign up to request clarification or add additional context in comments.

1 Comment

The "depends on your machine and compiler" is the part that should be emphasized. The C11 Standard - 6.2.4 Storage durations of objects speaks only to the time when an object can be validly accessed, not where it is allocated.
1

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. To build the assembler code, pass the -S argument:

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. 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. 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? If I am not mistake the EXE file size for the static version will be much bigger than the no static version. 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). I don't have a VM with windows to double check.

enter image description here

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.

answered May 28, 2018 at 22:47

3 Comments

I think they are talking about arr being defined inside a function
I don't know, the code I see is only the declaration and no more information. I have replayed the stack overflow with this code into my machine (x86_64 architecture). It is the only thing I can say.
@Alejandro Visiedo First,my code is inside a function . Second, I am using visual studio 2015 on x64 machine

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.