scope of variables

2020-07-22 16:31发布

Why do local variables use Stack in C/C++?

标签: c
7条回答
forever°为你锁心
2楼-- · 2020-07-22 17:08

Local variables are local to frames in the call stack.

Using a stack allows recursion.

查看更多
戒情不戒烟
3楼-- · 2020-07-22 17:08

Because stack is part of the memory that will be automatically discarged when the scope ends. This is the reason for calling sometimes local variables as "automatic". Local variable in a call are "insulated" from recursive or multithreaded calls to the same function.

查看更多
叛逆
4楼-- · 2020-07-22 17:13

Technically, C does not use a stack. If you look at the C99 standard, you'll find no reference to the stack. It's probably the same for the C++ standard, although I haven't checked it.

Stacks are just implementation details used by most compilers to implement the C automatic storage semantics.

查看更多
ゆ 、 Hurt°
5楼-- · 2020-07-22 17:16

Local data storage – A subroutine frequently needs memory space for storing the values of local variables, the variables that are known only within the active subroutine and do not retain values after it returns. It is often convenient to allocate space for this use by simply moving the top of the stack by enough to provide the space. This is very fast compared to heap allocation. Note that each separate activation of a subroutine gets its own separate space in the stack for locals.

Stack allocation is much faster since all it really does is move the stackpointer. Using memory pools you can get comparable performance out of heap allocation but that comes with a slight added complexity and its own headaches.

In Heaps there is another layer of indirection since you will have to go from stack -> heap before you get the correct object. Also the stack is local for each thread and is inherintly thread safe, where as the heap is free-for-all memory

查看更多
贪生不怕死
6楼-- · 2020-07-22 17:18

It depends on the implementation where variables are stored. Some computers might not even have a "stack" :D

Other than that, it is usual to do some house keeping when calling functions for keeping track of the return address and maybe a few other things. Instead of creating another house keeping method for local variables, many compiler implementations choose to use the already existing method, which implements the stack, with only minimal changes.

查看更多
萌系小妹纸
7楼-- · 2020-07-22 17:28

The question you're actually asking is, "why do C and C++ compilers use the hardware stack to store variables with auto extent?"

As others have mentioned, neither the C nor C++ language definitions explicitly say that variables must be stored on a stack. They simply define the behavior of variables with different storage durations:

6.2.4 Storage durations of objects

1 An object has a storage duration that determines its lifetime. There are three storage durations: static, automatic, and allocated. Allocated storage is described in 7.20.3.

2 The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address,25) and retains its last-stored value throughout its lifetime.26) If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime.

3 An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

4 An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration.

5 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.

C language standard, draft n1256.

No doubt that paragraph 5 was written with hardware stacks in mind, but there are oddball architectures out there that don't use a hardware stack, at least not in the same way as something like x86. The hardware stack simply makes the behavior specified in paragraph 5 easy to implement.

查看更多
登录 后发表回答