In C, working with global variables will help the stack?

No, I'm really serious.

I just witnessed someone moving variables locally into a function to a global status, with a β€œClear stack” commit message.

Is there any justification for this kind of thing?

+4
source share
6 answers

First, transferring variables to global variables does not directly improve CPU utilization. The initialization of the stack is usually a single addition / subtraction at the entrance / exit of the function, regardless of the size of the frame frame.

However, if a function requires a very large working set, it is better to put it on something other than a stack; stack size is usually limited. The usual choice is a bunch; however, it takes time to set aside and free, and therefore, if you often call this feature, it can be expensive. This is also a problem in embedded systems where they may not have the correct heap implementation.

So, if a bunch is a problem, global solutions may be a solution. However, they have their own drawbacks - in particular, you do not want several threads connected with global data to be executed at the same time, and you cannot execute this function without great care, otherwise recursive bits may damage previous function calls.

So, this is a method that in some cases can be useful. However, I would not recommend using it as a first choice due to threading issues.

Also, for what it's worth, you can get the same memory effects with static variables. I would recommend using them instead, as otherwise you will end up polluting the global namespace.

+10
source

There is this myth around the fact that global variables make your program "faster." That shit.

However, if you are using a large static array, you can not push it onto the stack to prevent stack overflows. The stack memory is significantly small compared to the total amount of logical memory.

In a well-designed program that has more than 100 LOC, you can use other allocation methods for your huge data: put it in an object (std :: vector, etc.) or use malloc / free, new / delete.

+3
source

If the variables were declared static , there is probably no difference.

If they were not, then there will be less clicking and popping up the stack, but the behavior of the program may change.

0
source

Using global variables instead of function parameters will reduce the required stack space for calling the function. Probably exactly where this comment comes from. But since all global variables live throughout the duration of the program, the total RAM consumption in the program will be higher.

This is not a myth, many types of systems have sluggish stacks. The effect will be much less noticeable on a PC or other such high-end 32/64 bit processors (nothing in the original message indicates a PC, though).

Global variables instead of parameters are usually very bad practice, as this leads to spaghetti. To avoid this, such variables should be declared static.

The best practice for reducing stack space when calling a function is pasting if you have this option.

0
source

If you are worried about stack overflow in recursion, and global variables can solve your problem, there is a much better solution: put all the variables in a struct in an external shell function for your recursion and pass a pointer to that struct to the actual recursive function. This approach gives you all the advantages of global variables and none of the disadvantages.

Also note that in modern systems with position-independent code and shared libraries, access to global variables is often much slower than access to local variables or access to structural elements indirectly through a single pointer due to the costs of relative GOT addressing, etc. .

Do not use global variables unless you really need a global state, and even then try to eliminate it. Never enter global variables as an "optimization".

0
source

Is there any justification for doing such things?

No.

-4
source

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


All Articles