mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-11-16 17:30:55 +00:00
[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
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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+)
8aefe54Add malloc, calloc and free wrappers to trace all of the allocations72e351bImprove memory leak reporting when multiple allocations occurs in the same file at the same line.9230e16Change directory name from 'debugging' to 'developer_tools' and added CMakeLists.txt, also change an include name to match a file name2b147a9Edit root CMakeLists.Txtcface3fUpdate developer_tools/malloc_dbg.hdb30cd5Edit CMakeLists.txt to create a static library from malloc_dbg.c3e1337aMerge branch 'debugging' of github.com:tinouduart33/C into debugging6220edaAdd comments for the includes86e8ac6Change test.c name to test_malloc_dbg.c, also edit CMakeLists.txt to link malloc_dbg.a only to test_malloc_dbg.1c9b0f6Change 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.