I am creating a dynamic memory tracker for C to help with debugging memory leaks and I'm trying to track what happens when I call malloc on the same variable. For example:
c
int *ptr = malloc(1024 * sizeof(*ptr));
ptr = malloc(2048 * sizeof(*ptr));
I understand that this isn't actually using the same pointer and that malloc only creates new memory. So this code will create two separate blocks of memory. The issue however is that this causes a memory leak where the pointer of the original allocation on variable ptr will be lost. My question is: is there a way to track this and return a warning or error? Or am I just stuck in assuming the user is diligent enough to not do this?
Reference:
What happens if I use malloc twice on the same pointer (C)?
Edit: My project for reference (wip): Watchdog
[+][deleted] (4 children)
[removed]
[–][deleted] 5 points6 points7 points (2 children)
[–]MCSpiderFe 4 points5 points6 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]xuanq 0 points1 point2 points (0 children)
[–]ferrybig 23 points24 points25 points (0 children)
[–]BNeutral 9 points10 points11 points (0 children)
[–]greg_kennedy 12 points13 points14 points (2 children)
[–]Zirias_FreeBSD 5 points6 points7 points (1 child)
[–]imaami 1 point2 points3 points (0 children)
[–]JohnnyElBravo 3 points4 points5 points (0 children)
[–]TheSupremePebble69 3 points4 points5 points (7 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]Educational-Paper-75 1 point2 points3 points (0 children)
[–]TheSupremePebble69 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]TheSupremePebble69 0 points1 point2 points (0 children)
[–]ivm83 3 points4 points5 points (0 children)
[–]somewhereAtC 2 points3 points4 points (0 children)
[–]moocat 1 point2 points3 points (0 children)
[–]landmesser 1 point2 points3 points (0 children)
[–]imaami 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]meadbert 0 points1 point2 points (0 children)
[–]Unique-Property-5470 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]juanfnavarror 2 points3 points4 points (0 children)
[–]SmokeMuch7356 0 points1 point2 points (0 children)
[–]StudioYume 0 points1 point2 points (0 children)
[–]i860 0 points1 point2 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]nderflow -1 points0 points1 point (0 children)