3

Given the fact that local variables inside a function in C get pushed to the stack once the function is called (After pushing the variables being passed to the function), is there any limit to the quantity of said variables before the stack buffer overflows? Or that limit is just given by the amount of RAM a determined host has?

I've tried to test this by creating a 4,6gb .C file with a single function having 25000*13 variables declared and initialized to 0. Said function is called inside main() but it compiled just fine (With -O0) and it didn't crash.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
4d4143
  • 73
  • 4
  • 2
    Default stack size limits are 1-8 MiB on Windows, macOS, and Linux. – Eric Postpischil Dec 05 '21 at 19:52
  • [Related.](https://stackoverflow.com/questions/47211957/are-too-large-objects-with-automatic-storage-duration-undefined-behaviour/47212027#47212027) – Eric Postpischil Dec 05 '21 at 19:54
  • What *problem do you hope to solve* by learning about this? – Karl Knechtel Dec 05 '21 at 19:55
  • @Passerby The "hat" symbol is also used for exponentiation. I got confused though. So it was really "*" instead of "^". – 4d4143 Dec 05 '21 at 19:57
  • 3
    @KarlKnechtel Does there need to be a problem to be solved? Seeking deeper understanding is an admirable goal. – John Kugelman Dec 05 '21 at 19:57
  • @Pablo That's a very good observation. I haven't yet! What should I expect to see if I gdb or radare2? lots of push ops right? – 4d4143 Dec 05 '21 at 19:59
  • See http://port70.net/~nsz/c/c11/n1570.html#5.2.4.1 for some *minimum* numbers of identifiers that compilers are supposed to support. – Shawn Dec 05 '21 at 20:04
  • @JohnKugelman deeper understanding is an admirable goal, but the purpose of this site is to solve practical programming problems. It is not practical to write a function with thousands of local variables. (Your brain would not be able to keep track of them all.) Maybe a code generator could do it. – Raymond Chen Dec 05 '21 at 20:27
  • sometims only a few are enough to kill the program :) https://godbolt.org/z/qGvWYWosT – 0___________ Dec 05 '21 at 20:41
  • @RaymondChen this site is for Q&A. Period. Nothing states the questions have to be practical. Are we seriously discouraging someone from gaining a deeper understanding of compilers on SO? – Qix - MONICA WAS MISTREATED Dec 05 '21 at 21:15
  • @Qix-MONICAWASMISTREATED Right there in big letters on the [Tour](https://stackoverflow.com/tour): "Get answers to practical, detailed questions". Followed by "Focus on questions about an **actual problem** you have faced." Is having a function with over 300,000 local variables an actual problem you have faced? – Raymond Chen Dec 05 '21 at 21:33
  • @RaymondChen that's not, and has never been, the spirit of the site. This is a completely valid question. – Qix - MONICA WAS MISTREATED Dec 06 '21 at 08:40
  • @Qix-MONICAWASMISTREATED You might believe it isn't, but that's [what it has said on the "Tour" page since the site opened in 2013](http://web.archive.org/web/20131205092051/stackoverflow.com/tour). Practical problems you have actually encountered, not theoretical ones. Also the first sentence on "[What types of questions should I avoid asking?](https://stackoverflow.com/help/dont-ask)": "You should only ask practical, answerable questions based on actual problems that you face." – Raymond Chen Dec 06 '21 at 14:54

3 Answers3

8

For example according to the C Standard (5.2.4.1 Translation limits)

1 The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:

— 4095 external identifiers in one translation unit

— 511 identifiers with block scope declared in one block

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    That is minimum. It does not answer what is the maximum :) – 0___________ Dec 05 '21 at 20:34
  • 1
    @0___________ Maximum is implementation defined. – Vlad from Moscow Dec 05 '21 at 20:34
  • And 127 nested blocks. So 511*127 local variables? Plus 127 function arguments... – Shawn Dec 05 '21 at 21:14
  • @Shawn The standard does not require a compiler to support all limits being reached simultaneously. "The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits." So there must be one program with 511 identifiers with block scope that can be successfully compiled and executed. And also oneprogram with 127 nested blocks that can be successfully compiled and executed. But they need not be the same program. – Raymond Chen Dec 05 '21 at 21:36
2

The default stack size for the main thread is 8 MiB on macOS (from the ld man page), 2 MiB on Linux (per the --stack switch here), and 1 MiB on Windows (per this page). Additional threads created within the program may have smaller stacks. (Note: The various sources cited use “MB” or “Mb”; I have interpreted these in context to mean 1,048,576 bytes = 1 MiB.)

A different stack size can be requested when linking the program into an executable file.

Functions use stack space for various things, including passing arguments, establishing stack frames for throwing exceptions, sometimes debugging information, and temporary work space, so there is not a direct correlation between the amount of memory used for the stack and the number of variables that can be defined.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

As you do not ask about the minimum requirements I will write about the C standard

Modern conforming compilers nowadays do not set any limits (Microsoft does not count as it is not conforming). You are limited by the available computer resources.

Another part of the toolchain (linker, object and executable files limitations etc) might put some additional limits, but modern mainline compilers usually do not have any.

0___________
  • 60,014
  • 4
  • 34
  • 74