# 04. Stack and Heap Memory

### Comparison of Stack and Heap memory

| Stack Memory                                                               | Heap Memory                                                                        |
| -------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| Generally, memory is automatically managed by program.                     | Generally, memory management to be done by programmer.                             |
| Traditionally used to stores temporary variables created by each function. | Variables created on the heap are accessible by any function, anywhere in program. |
| Limit on stack memory size.                                                | No specific limit on heap memory size.                                             |
| Self cleaning memory.                                                      | Memory cleanup required explicitly.                                                |

### Gophercon - Understanding Allocations: the Stack and the Heap - GopherCon SG 2019

{% embed url="<https://www.youtube.com/watch?v=ZMZpH4yT7M0>" %}
Like C, Go uses both stack and heap memory. How can a Gopher know which is being used?&#x20;
{% endembed %}

### Blog Posts

{% embed url="<https://www.ardanlabs.com/blog/2013/07/understanding-pointers-and-memory.html>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.codewithgo.com/stack-and-heap-memory.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
