AllExperts > Encyclopedia 
Search      
Find out about volunteering to AllExperts

Stack-based memory allocation: Encyclopedia BETA


Free Encyclopedia
 Index · Browse A-Z  · Questions and Answers ·
Encyclopedia

Browse A-Z
ABCDEFGHIJKLMNOPQRSTUVWXYZNum


License
Disclaimer

 
 
 
 
Free Online Courses
12 Weeks to Weight Loss
Take Charge of Stress
Learn How to Bake
Budgeting 101
Deeper Faith
DIY Fashion Makeover

       MORE E-COURSES
 
   

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z  Misc

Stack-based memory allocation

Stacks in computing architectures are regions of memory where data is added or removed in a Last-In-First-Out manner.

In most modern computer systems, each thread has a reserved region of memory referred to as its stack. When a function executes, it may add some of its state data to the top of the stack; when the function exits it is responsible for removing that data from the stack. If a region of memory lies on the thread's stack, that memory is said to have been allocated on the stack.

Because the data is added and removed in a last-in-first-out manner, stack allocation is very simple and typically faster than heap allocation. Another advantage is that memory on the stack is automatically reclaimed when the function exits, which can be convenient for the programmer.

A disadvantage of stack based memory allocation is that a thread's stack size can be as small as a few dozen kilobytes. Allocating more memory on the stack than is available can result in a crash or stack overflow. Another disadvantage is that the memory stored on the stack must be deallocated when the function that created it returns, so that other functions that want to make use of it beyond its lifetime must copy the data.

If the address of the top of the stack decreases when more data is added to it, the stack is said to "grow down," and if the addresses increases the stack is said to "grow up." A downwards growing stack makes it easier to exploit buffer overflows, because array accesses typically go upwards in memory, and thus buffer overflows will read or write previous data on the stack. Unfortunately, in most modern operating systems, the stack starts with a high address and grows down.

Some processors families, such as the x86, have special instructions for manipulating the stack of the currently executing thread. Other processor families, including PowerPC and MIPS, do not have explicit stack support, but instead rely on convention and delegate stack management to the operating system's ABI.

See also

*Heap-Based Memory Allocation



Email this page
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.
This is the "GNU Free Documentation License" reference article from the English Wikipedia. All text is available under the terms of the GNU Free Documentation License. See also our Disclaimer.