Why is there a stack space in C ++ allocated for local variable declarations that are never met by the execution thread?

Why is there a stack space in C ++ allocated for local variables that are never encountered by the execution thread? Or, if left undefined by the C ++ standard, why do some compilers allocate stack space for local variable declarations that are never encountered by the execution thread? Can the compiler allocate stack space for variable declarations that occur in the thread of execution and are still working?

To illustrate, calling this function in debug mode, where variable characters cannot be detected, leads to a stack overflow:

void f() { if (false) { char chars[INT_MAX]; } } 
+6
source share
5 answers

1) There is no reason not to do this. The C ++ standard does not promise that variables inside areas that are not entered at run time will not be allocated to them.

2) It is faster and easier. If all locales are selected at once, the code for allocating space for locales consists of one update to the stack pointer at the beginning of the function and one at the end. If locales within an area should be allocated and deallocated at the beginning and end of that area, you will receive much more stack pointer updates.

+6
source

The compiler has executed the declaration of a local variable. Whether this is being implemented is not defined by the standard, so the behavior is implementation-specific . This allows you to immediately allocate space for all local variables, which reduces overhead.

However, during optimization, the compiler identifies your specific case as dead code, and what you encounter will be eliminated.

+8
source

This will be extremely dependent on the compiler and optimization level. Some compilers might say that if-block would never be reached, and thus would not generate code for it at all. Others will be. Others may depend on the level of optimization. Still others allocate space for all local variables when calling a function, not when entering a region, because it is "easier" to calculate. This actually looks like the behavior of vanilla C, since there you SHOULD declare all the variables at the beginning of the functions (not sure if the spanning regions with local scale variables exist in vanilla C, they have been around for a long time), so this could be a deterrent.

In addition, you are obviously faced with the fact that the FAR stack is smaller than the heap, and how the declaration of a local variable may exceed its capacity, but I'm sure the intent of your example.

If you are looking for the best β€œwhy they did it”, then publish the version (and settings) of the compiler that you use for this, and perhaps one of the authors will answer. Without this, who knows?

+4
source

In the case of MSVC, a compiler warning (C4101) is issued for unused local variables. More details: http://msdn.microsoft.com/en-us/library/c733d5h9(v=vs.80).aspx

0
source

It depends on the compiler and the settings you use. For example (I had to change this so that it really compiles in VS 2008), the following will throw an exception in Debug, but end up in Release:

 void f() { if (false) { char chars[INT_MAX/2]; } } int main(int argc, _TCHAR* argv[]) { f(); return 0; } 
0
source

Source: https://habr.com/ru/post/913058/


All Articles