Stack (memory)

From Cppwiki

Jump to: navigation, search

The stack is an area of memory, usually one per thread, used to store function-local objects. Although the C++ standard does not discuss the existance of a stack, nearly all implementations have a stack of some form.

int f() {
  int *i = new int; // int is allocated from the heap
  int j; // int is allocated on the stack

When a function returns, any stack allocations are automatically freed.

alloca

Some implementations support a non-standard function called alloca (sometimes _alloca) which allocates memory from the stack. It is often faster than new or malloc, and the memory is freed automatically when the function returns. However, the difficulty of implementing alloca on certain platforms has prevented it from becoming standard.

Personal tools