All Topics  
Stack-based memory allocation

 

   Email Print
   Bookmark   Link






 

Stack-based memory allocation



 
 
Stack
Stack (data structure)

In computer science, a stack is an abstract data type and data structure based on the principle of LIFO . Stacks are used extensively at every level of a modern computer system....
s 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
Thread (computer science)

In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process....
 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.






Discussion
Ask a question about 'Stack-based memory allocation'
Start a new discussion about 'Stack-based memory allocation'
Answer questions from other users
Full Discussion Forum



Encyclopedia


Stack
Stack (data structure)

In computer science, a stack is an abstract data type and data structure based on the principle of LIFO . Stacks are used extensively at every level of a modern computer system....
s 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
Thread (computer science)

In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process....
 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
Crash (computing)

A crash or in computing is a condition where a program stops performing its expected function and also stops responding to other parts of the system....
 due to stack overflow
Stack overflow

In software, a stack overflow occurs when too much computer memory is used on the call stack. In many programming languages the call stack contains a limited amount of memory, usually determined at the start of the program....
. Another disadvantage is that the memory stored on the stack is automatically deallocated when the function that created it returns, and thus the function must copy the data if they should be available to other parts of the program after it returns.

Some processors families, such as the x86, have special instructions for manipulating the stack of the currently executing thread. Other processor families, including PowerPC
PowerPC

PowerPC is a RISC instruction set architecture created by the 1991 Apple Inc.?IBM?Motorola alliance, known as AIM alliance. Originally intended for personal computers, PowerPC CPUs have since become popular embedded system and high-performance processors....
 and MIPS
MIPS architecture

MIPS is a RISC instruction set architecture developed by MIPS Technologies . In the mid to late 1990s, it was estimated that one in three RISC microprocessors produced were MIPS implementations....
, do not have explicit stack support, but instead rely on convention and delegate stack management to the operating system's Application Binary Interface (ABI)
Application binary interface

In computer software, an application binary interface describes the low-level interface between an application program and the operating system or an other application....
.

See also

  • Call stack
    Call stack

    In computer science, a call stack is a dynamic Stack data structure that stores information about the active subroutines of a computer program....
  • Dynamic memory allocation
    Dynamic memory allocation

    In computer science, dynamic memory allocation is the allocation of computer storage storage for use in a computer program during the runtime of that program....
  • Stack buffer overflow
    Stack buffer overflow

    In software, a stack buffer overflow occurs when a program writes to a computer memory address on the program's call stack outside of the intended data structure; usually a fixed length buffer....
  • Stack machine
    Stack machine

    In computer science, a stack machine is a model of computation in which the computer's memory takes the form of one or more stack s. The term also refers to an actual computer implementing or simulating the idealized stack machine....