[PR #782] [MERGED] feat: Get a trace of all of the allocations, using a doubly linked list and wrappers for malloc/calloc and free. #1299

Open
opened 2026-01-29 15:19:04 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/TheAlgorithms/C/pull/782
Author: @tinouduart33
Created: 1/24/2021
Status: ✅ Merged
Merged: 2/17/2021
Merged by: @Panquesito7

Base: master ← Head: debugging


📝 Commits (10+)

  • 8aefe54 Add malloc, calloc and free wrappers to trace all of the allocations
  • 72e351b Improve memory leak reporting when multiple allocations occurs in the same file at the same line.
  • 9230e16 Change directory name from 'debugging' to 'developer_tools' and added CMakeLists.txt, also change an include name to match a file name
  • 2b147a9 Edit root CMakeLists.Txt
  • cface3f Update developer_tools/malloc_dbg.h
  • db30cd5 Edit CMakeLists.txt to create a static library from malloc_dbg.c
  • 3e1337a Merge branch 'debugging' of github.com:tinouduart33/C into debugging
  • 6220eda Add comments for the includes
  • 86e8ac6 Change test.c name to test_malloc_dbg.c, also edit CMakeLists.txt to link malloc_dbg.a only to test_malloc_dbg.
  • 1c9b0f6 Change comment style and change EXIT_SUCCESS to 0

📊 Changes

5 files changed (+392 additions, -0 deletions)

View changed files

📝 CMakeLists.txt (+1 -0)
➕ developer_tools/CMakeLists.txt (+35 -0)
➕ developer_tools/malloc_dbg.c (+289 -0)
➕ developer_tools/malloc_dbg.h (+36 -0)
➕ developer_tools/test_malloc_dbg.c (+31 -0)

📄 Description

Hello, i created a new directory named "debugging". In this directory, i have created three new files : malloc_dbg.c, malloc_dbg.h and test.c

The main objective of these files is to know where a memory leak occurred (even on a Windows computer where valgrind doesn't exists and without having to use WSL). The test.c only contains a main function in which we call malloc and calloc and we only free one of the two pointers.

In malloc_dbg.h there are three macros : malloc, calloc and free.
There are also the prototypes of the functions malloc_dbg, calloc_dbg, free_dbg and printLeaks.

The malloc_dbg.c file contains the definitions of the above functions and a structure (a doubly linked list which is used to store a pointer returned by malloc/calloc, the number of bytes that have been allocated, the file from where malloc/calloc was called and the line number of this call).

malloc_dbg and calloc_dbg functions both calls atexit() to make sure the leaks will be printed at the end of the execution.

In test.c file, we simply include malloc_dbg.h to be able to use the macros.

When the program exits, it should print if there are memory leaks or no, for example :
"Memory Leaks detected.

256 bytes lost
address : 0x0x558c8a33e310 in main test.c:23

====================================
SUMMARY :
256 bytes lost in 1 blocks"

This has been tested under Linux and Windows.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/TheAlgorithms/C/pull/782 **Author:** [@tinouduart33](https://github.com/tinouduart33) **Created:** 1/24/2021 **Status:** ✅ Merged **Merged:** 2/17/2021 **Merged by:** [@Panquesito7](https://github.com/Panquesito7) **Base:** `master` ← **Head:** `debugging` --- ### 📝 Commits (10+) - [`8aefe54`](https://github.com/TheAlgorithms/C/commit/8aefe547df3c9bae2185236d5077b9d3e21af8a2) Add malloc, calloc and free wrappers to trace all of the allocations - [`72e351b`](https://github.com/TheAlgorithms/C/commit/72e351be267484db213a35269bbc75235921b519) Improve memory leak reporting when multiple allocations occurs in the same file at the same line. - [`9230e16`](https://github.com/TheAlgorithms/C/commit/9230e16e36aca7e8900034451928b2f7b525a4f1) Change directory name from 'debugging' to 'developer_tools' and added CMakeLists.txt, also change an include name to match a file name - [`2b147a9`](https://github.com/TheAlgorithms/C/commit/2b147a9619839411fbd095b4c5ad3d22b9b0f64d) Edit root CMakeLists.Txt - [`cface3f`](https://github.com/TheAlgorithms/C/commit/cface3f4a1f96bb29f54479c19ee85036e7239df) Update developer_tools/malloc_dbg.h - [`db30cd5`](https://github.com/TheAlgorithms/C/commit/db30cd5cb8351a3f7be9e76a06bf29ccefd3e488) Edit CMakeLists.txt to create a static library from malloc_dbg.c - [`3e1337a`](https://github.com/TheAlgorithms/C/commit/3e1337a85a0fe56216e5a6c886fdf8daa38d082b) Merge branch 'debugging' of github.com:tinouduart33/C into debugging - [`6220eda`](https://github.com/TheAlgorithms/C/commit/6220edad719a5956b5a552bb679cfd4619545d0f) Add comments for the includes - [`86e8ac6`](https://github.com/TheAlgorithms/C/commit/86e8ac649139bd2b3421b3e62ca47e3207445e48) Change test.c name to test_malloc_dbg.c, also edit CMakeLists.txt to link malloc_dbg.a only to test_malloc_dbg. - [`1c9b0f6`](https://github.com/TheAlgorithms/C/commit/1c9b0f61dc928dcdfc3eff1956832a4675bda2e8) Change comment style and change EXIT_SUCCESS to 0 ### 📊 Changes **5 files changed** (+392 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+1 -0) ➕ `developer_tools/CMakeLists.txt` (+35 -0) ➕ `developer_tools/malloc_dbg.c` (+289 -0) ➕ `developer_tools/malloc_dbg.h` (+36 -0) ➕ `developer_tools/test_malloc_dbg.c` (+31 -0) </details> ### 📄 Description Hello, i created a new directory named "debugging". In this directory, i have created three new files : malloc_dbg.c, malloc_dbg.h and test.c The main objective of these files is to know where a memory leak occurred (even on a Windows computer where valgrind doesn't exists and without having to use WSL). The test.c only contains a main function in which we call malloc and calloc and we only free one of the two pointers. In malloc_dbg.h there are three macros : malloc, calloc and free. There are also the prototypes of the functions malloc_dbg, calloc_dbg, free_dbg and printLeaks. The malloc_dbg.c file contains the definitions of the above functions and a structure (a doubly linked list which is used to store a pointer returned by malloc/calloc, the number of bytes that have been allocated, the file from where malloc/calloc was called and the line number of this call). malloc_dbg and calloc_dbg functions both calls atexit() to make sure the leaks will be printed at the end of the execution. In test.c file, we simply include malloc_dbg.h to be able to use the macros. When the program exits, it should print if there are memory leaks or no, for example : "Memory Leaks detected. 256 bytes lost address : 0x0x558c8a33e310 in main test.c:23 ==================================== SUMMARY : 256 bytes lost in 1 blocks" This has been tested under Linux and Windows. <a href="https://gitpod.io/#https://github.com/TheAlgorithms/C/pull/782"><img src="https://gitpod.io/api/apps/github/pbs/github.com/tinouduart33/C.git/86e8ac649139bd2b3421b3e62ca47e3207445e48.svg" /></a> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 15:19:04 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/C#1299