Hi.
I have 2 functions, for most things I understand, but can someone tell me why are we using allocbuf in the cycle:
Code:
if (allocbuf + ALLOCSIZE - allocp >= n)
allocbuf is an array and the name of the array is a pointer to the first element &allocbuf [0]. I understand the point of removing the pointer's current position from the whole ALLOCSIZE and comparing if the remainder is greater than what we need.
Also what is the point of the same thing in the following cycle:
Code:
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
Which pointer are we suppose to feed to afree ()? If we feed it allocp it will free the space after allocp's current location, if we give it the first element of &allocbuf [0], it will free the whole buffer?
The whole code is here:
Code:
/* Allocation functions. */
#define ALLOCSIZE 10000 // Size of available space.
static char allocbuf [ALLOCSIZE]; // Storage for alloc ().
static char *allocp = &allocbuf [0]; // Next free position.
char *alloc (int n) // Return pointer to n characters.
{
if (allocbuf + ALLOCSIZE - allocp >= n){// It fits.
allocp += n;
return allocp - n; // Old p.
} else // Not enough room.
return 0;
}
void afree(char *p) // Free storage pointed to by p.
{
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
}
/* Allocation functions END. */
Please reply with examples.