What and where are the stack and heap?

翻译 2015年11月17日 20:45:38

The stack is the memory set aside as scratch space for a thread of execution. When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data. When that function returns, the block becomes unused and can be used the next time a function is called. The stack is always reserved in a LIFO (last in first out) order; the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack; freeing a block from the stack is nothing more than adjusting one pointer.

The heap is memory set aside for dynamic allocation. Unlike the stack, there's no enforced pattern to the allocation and deallocation of blocks from the heap; you can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time; there are many custom heap allocators available to tune heap performance for different usage patterns.

Each thread gets a stack, while there's typically only one heap for the application (although it isn't uncommon to have multiple heaps for different types of allocation).

To answer your questions directly:

To what extent are they controlled by the OS or language runtime?

The OS allocates the stack for each system-level thread when the thread is created. Typically the OS is called by the language runtime to allocate the heap for the application.

What is their scope?

The stack is attached to a thread, so when the thread exits the stack is reclaimed. The heap is typically allocated at application startup by the runtime, and is reclaimed when the application (technically process) exits.

What determines the size of each of them?

The size of the stack is set when a thread is created. The size of the heap is set on application startup, but can grow as space is needed (the allocator requests more memory from the operating system).

What makes one faster?

The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free. Also, each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor's cache, making it very fast. Another performance hit for the heap is that the heap, being mostly a global resource, typically has to be multi-threading safe, i.e. each allocation and deallocation needs to be - typically - synchronized with "all" other heap accesses in the program.

What and where are the stack and heap?

The stack is the memory set aside as scratch space for a thread of execution. When a function is cal...

What is heap and stack?

The stack is a place in the computer memory where all the variables that are declared and initialize...

Structure of a C-Program in Memory | How Heap,Stack,Data and Code segments are stored in memory?

This is most confusing question for and beginner of c programming language.  They dont know theall s...

What-are-ontologies-and-why-we-need-them

  • 2010年01月15日 13:17
  • 770KB
  • 下载

What are training set, validation set and test set?

这三个名词在机器学习领域的文章中极其常见,但很多人对他们的概念并不是特别清楚,尤其是后两个经常被人混用。Ripley, B.D(1996)在他的经典专著Pattern Recognition and ...

The Run-time Heap and Stack

  • 2011年05月11日 21:11
  • 72KB
  • 下载

What are the differences between Apache Kafka and RabbitMQ?

FROM https://www.quora.com/What-are-the-differences-between-Apache-Kafka-and-RabbitMQ  Kafka is...

What are the differences between a pointer variable and a reference variable in C++?

Question: I know references are syntactic sugar, so code is easier to read and write. But wh...

The State of Visual Analytics Views on what visual analytics is and where it is going

This paper appears in: Visual Analytics Science and Technology (VAST), 2010 IEEE Symposium on.   现...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:What and where are the stack and heap?
举报原因:
原因补充:

(最多只允许输入30个字)