# HEAP

## What is the HEAP ?&#x20;

Heap is a memory region allocated to every program. Unlike stack, heap can be dynamically allocated.

The program can "request" and "release" memory from the heap whenever it requires.

This allocated memory is global. it mean that it can be accessed and modified from anywhere within a program. This can be done using `pointers` to reference the dynamically allocated memory.

{% hint style="info" %}
Note that the **HEAP** permits to use **largest data** than the stack and theses data are **accessible from anywhere**.&#x20;

Theses positives points are followed by a significant negative one : **the HEAP is slower than the stack.**&#x20;
{% endhint %}

## How to use ?

In C, `stdlib.h` provides functions to work with the HEAP.&#x20;

* **`malloc()`**, used to request the space memory
* **`free()`**, used to release the space memory

Here is a code example :&#x20;

```c
// Dynamically allocate 10 bytes
char *buffer = (char *)malloc(10);

strcpy(buffer, "hello");
printf("%s\n", buffer); // prints "hello"

// Frees/unallocates the dynamic memory allocated earlier
free(buffer);
```


---

# 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://www.ctfrecipes.com/pwn/general-knowledge/operation-of-the-heap.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.
