I am trying to write simple memory manager (I should really say memory tracker) in my C program.
I am basically creating doubly linked list of allocated blocks where I put pointers on previous and next on start of each block. Malloc procedure looks like this:
typedef struct MemUnit TMemUnit;
struct tMemUnit
{
TMemUnit *prev;
TMemUnit *next;
}
TMemUnit *new = malloc(sizeof(TMemUnit) + wantedSize);
if (new == NULL)
/* ERROR */
else
{
if (memFirst == NULL) {
memFirst = new;
memLast = new;
new->prev = NULL;
new->next = NULL;
} else {
new->prev = memLast;
new->next = NULL;
memLast->next = new;
memLast = new;
}
return (void *)(new + sizeof(TMemUnit));
The problem is segmentation faults on places where was none before.
Valgrind also gives Invalid read/write errors.
==22872== Invalid write of size 4
==22872== at 0x400FF1: main (test-memory.c:40)
==22872== Address 0x54d51b0 is not stack'd, malloc'd or (recently) free'd
When I print addresses of allocated block (wantedSize = 20 * sizeof(int), trying to write first int) for this error they look ok:
new --> 0x54d5030
new + sizeof(TMemUnit) + wantedSize --> 0x54d5430
I can't figure out where is my mistake.
Thanks
new
. – Jonathon Reinhart