answersLogoWhite

0


Best Answer

Because they are created and destroyed on 'last-in-first-out' principle.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why are local variable stored on stack?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Where the local variables will be stored?

When you declare a variable and it's data type in a function, it is stored in the specific space for memory allocated by the variable type identifier known as the "stack."


Where does Local Variables get stored in?

Stack.


Local Variables will get stored in?

Stack.


Where local variables are stored in c?

On the stack.


Where string is stored on Heap or Stack in java?

A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.A String is treated as an object, meaning there is an object on the heap. Of course, the variable you define is a pointer to the object, and it is stored on the stack.


Where are the auto variable stored?

In procedural programming languages like C auto variables have a lifetime bound to their scope inside a function, and is often synonymous with local variable. They're commonly allocated in the current stack frame along with a function's arguments and a possible return value.


Where auto variables are stored?

Auto variables are stored on the stack alongside all other local variables.


Can variables be changed?

A static variable is one which is not stored on the stack but in the memory of the program. Static variables can be changed.


Where does global variables stored in C?

It depends entirely on what platform you are using. In an embedded environment, for instance global/static variables go into different RAM memory segments depending on whether or not they are initialised. constants are often left in ROM automatic variables are normally placed of the stack of the currently running task but not always.


Local Variables is stored in which part of the memory?

RAM = Random Access Memory


Where do objects and references stored in memory in java?

Objects are stored in an area of memory called the "heap", whilst reference variables are stored in the "stack". These are both parts of the computer's RAM memory; the Java Virtual Machine (JVM) simply reserves part of the memory available to it for the stack and for the heap, and manages them accordingly.


What is difference between heap memory and stack memory?

Difference between Stack vs Heap memory"Stack memory" and "Heap memory" are physically the same. The same chip of RAM may be used as stack memory when running one program and later used as heap memory when running some other program.The difference is in how they are used.StackOften a function or method calls another function which in turn calls another function etc.The execution of all those functions remains suspended until the very last function returns its value.All the information required to resume the execution of these functions is stored on the stack.In particular, local variables are stored on the stack.Local variables are often stored for short amounts of time while a function/method block uses them to compute a task.Once a function/method has completed its cycle, the space on the stack used by all local variables is freed.This chain of suspended function calls is the stack, because elements in the stack (function calls) depend on each other.The stack is important to consider in exception handling and thread executions.HeapHeapThe heap is simply the memory used by programs to store global variables.Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.All global variables are stored in heap memory.All variables dynamically created by the program with "new()" or "malloc()" or similar commands are also stored on the heap.In some programming languages, all instances of an object, including all the attributes of that instance, are stored on the heap.In those programming languages, local variables of a function that have object type are implemented as creating the new object on the heap,and storing a reference to that object in the local variable, which is on the stack.When that function exits, the heap memory used by each local variable that has object is freed, and then all the stack used by that stack is freed.ComparisonA Heap reference is also stored in Stack memory until the life cycle of the object has completed. Inside the Heap reference all the the contents of the object are stored whereas with a local variable only the variable contents are stored in the stack.Example:Stackvar bluevar redref 0x456783 (Heap reference)var tomref 0x498702 (Heap reference)var dianeHeap (0x456783)name => Susanage => 26city => Londonheight => 5'7sex => femaleHeap (0x498702)name => Paulage => 21city => Glasgowheight => 6'0sex => maleIn addition to heap memory and stack memory, a completely separate section of memory stores the constructors and other methods of a class/object.