I am reading K&R pointers section 5.4 where a stack implementation of malloc()
and free()
are done. I am using gdb to debug the code, and the alloc()
part is working as expected. But for the afree()
part, the pointers are still pointing to the same location as before. Here is the code :
#include <stdio.h>
#define ALLOCSIZE 10000
static char allocbuf[ALLOCSIZE];
static char* allocp = allocbuf;
char* alloc(int n)
{
if(allocbuf + ALLOCSIZE - allocp >= n)
{
allocp += n;
return (allocp - n);
}
else
return 0;
}
void afree(char* p)
{
if(p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
}
int main()
{
char* a = alloc(10);
a = "ayushnair";
char*b = alloc(5);
b = "snea";
printf("%p %p\n", a, b);
afree(b);
afree(a);
printf("%p %p\n", a, b);
return 0;
}
New allocation
allocp 0x601080
after char* a = alloc(10);
allocp 0x60108a
after char* b = alloc(5);
allocp 0x60108f
after afree(b);
allocp 0x60108f
after afree(a);
allocp 0x60108f
allocp
still points to 0x60108f
. Why does it not update according to the code?
In your code, by saying
a = "ayushnair";
you're not storing the "ayushnair"
into the memory pointed by a
. The "ayushnair"
is a string literal and by saying
a = "ayushnair";
you're storing the base address of the string literal into a
. This way, you're essentially overwriting the returned pointer by the call to alloc()
.
This is not something you want. You may need to use strcpy()
to copy the string literal into the returned pointer.
That said, as per the current code, later by calling
afree(b);
afree(a);
you're invoking undefined behavior as you're trying to comapre pointers that do not point to the same object.
Quoting C11
, chapter §6.5.8, Relational operators
When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.