Atul
Atul

Reputation: 1177

scope of variables

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

Upvotes: 3

Views: 643

Answers (7)

Algorithmist
Algorithmist

Reputation: 6695

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

Upvotes: 2

S..K
S..K

Reputation: 2024

Local variables are limited to the scope in which they can be accessed. Using a stack enables jump of control from one scope to other and on returning, to continue with the local variables present initially.

When there is jump, the local variables are pushed and the jump is executed. On returning back to the scope, the local variables are popped out.

Upvotes: 0

John Bode
John Bode

Reputation: 123468

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.

Upvotes: 2

JeremyP
JeremyP

Reputation: 86651

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.

Upvotes: 6

pmg
pmg

Reputation: 108988

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.

Upvotes: 1

Felice Pollano
Felice Pollano

Reputation: 33252

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.

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86411

Local variables are local to frames in the call stack.

Using a stack allows recursion.

Upvotes: 0

Related Questions