Static and global variable in memory

  • Are the static variables stored on the stack similar to global ones? If so, how are they protected to allow access only to the local class?

  • In a multi-threaded context, there is a fear that other threads / kernel can directly access this memory? or why can't we use static / global in a variety of processes / threads?

+3
source share
4 answers

Variables stored on the stack are temporary. They belong to the function, etc., And when the function returns and the corresponding stack stack crashes, the stack variables disappear with it. Since global objects are intended to be accessed everywhere, they should not go out of context and, therefore, are stored on the heap (or in a special section of the binary file data), and not on the stack. The same goes for static variables; since they must hold their value between function calls, they cannot disappear when the function returns, therefore they cannot be allocated on the stack.

Regarding the protection of static variables, IIRC is mostly done by the compiler. Although the variable is on the heap, your compiler knows the limited context in which the variable is valid, and any attempt to access static from outside this context will result in an "unknown identifier" or similar error. The only other way to access the heap variable is incorrect if you know the static address and you blindly delete the link to the pointer to it. This should result in a memory access error at runtime.

In a multi-threaded environment, you can still use global variables and static variables. However, you must be much more careful. You must ensure that only one thread can access the variable at a time (usually through some kind of locking mechanism such as a mutex). In the case of local static variables inside a function, you need to make sure that your function will still function properly if it is called from multiple threads sequentially (i.e. it is called from thread 1 and then from thread 2, then thread 1, then thread 2 etc. etc.). This is usually more complex, and many functions that rely on static member variables are not thread safe because of this ( strtok is a notable example).

+3
source

Static variables have a static storage duration, so they usually do not push on the stack. The only "protection" for them is that their name has local visibility at compile time. Passing the address of a static variable gives it access.

The use of static / globals in a multi-threaded situation is due to the fact that if one thread modifies a variable at the same time as it tries to read it (for one example only), what can be read may be bad data.

+3
source

Are the stable variables stored on the stack similar to the global ones? If so, how are they protected to allow access only to the local class?

As a rule, they are stored in memory along with global ones. however, the visibility of variable names restricts access.

In a multi-threaded context, there is a fear that other threads / kernel can directly access this memory? or why can't we use static / global in a multiprocessor / streaming environment?

The problem is that there is only one copy of the static element, therefore, if several threads change this variable, one thread can lead to the loss of another thread change if there are no protective (critical sections) to prevent this.

+2
source

Are the static variables stored on the stack similar to global ones? If so, how are they protected to allow access only to the local class?

No, static refers only to storage duration - they can be global or have a local scope. Globals have static storage.

In a multi-threaded context, there is a fear that other threads / kernel can directly access this memory? or why can't we use static / global in multiple processes / threads?

Several authors will introduce ambiguity. You need to protect shared resources with a mutex or some such blocking mechanism.

+1
source

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


All Articles