> For the complete documentation index, see [llms.txt](https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/18-static-and-dynamic-allocation/bonus/memory-leaks.md).

# Memory Leaks

#### What is Memory Leak?

Memory leaks occur when we allocate memory using `new` but forget to deallocate it before the pointer to this memory goes out of scope.&#x20;

#### How does it occur?

Sometime we might remember to deallocate the memory but **use the wrong delete syntax.**&#x20;

```cpp
int *p1 = new int;
delete []p1;


int *p2 = new int[10];
delete p2;
```

This does not throw an error. But leaves both the variable and the array undeleted.&#x20;

Arrays should use `delete []` and varibales should use `delete`

#### What are the risks?

* Decrease the performance
* In worst case, no more space is left; program/system crashes
* More at risk: Video games, small embedded devices, device drivers, programs allocating and deallocating too frequently

<details>

<summary><code>References</code></summary>

* **Memory leak - Wikipedia. (2007). Retrieved 24 May 2022, from <https://en.wikipedia.org/wiki/Memory\\_leak>**
* **Memory leak in C++ and How to avoid it?. (2019). Retrieved 24 May 2022, from <https://iq.opengenus.org/memory-leak-in-cpp-and-how-to-avoid-it/>**

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/18-static-and-dynamic-allocation/bonus/memory-leaks.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
