The referenced code from
The C Programming Language, 2nd edition, page 101, simply allocates
n number of bytes from a predefined character array, allocbuf[] which has been initially allocated 1000 bytes of char for storage.
Essentially the routine:
Packs one of more smaller character arrays into the predefined:
Code:
static char allocbuf[ALLOCSIZE];
With each of the smaller character arrays accessible by the pointer returned by the routine.
Each successive call of:
First checks the current position of the pointer:
In relation to the end of the allocbuf, to ensure there is sufficient space to allocate
n bytes/char:
Code:
if(allocbuf + ALLOCSIZE - allocp >= n)
If there is sufficient space, the pointer allocp is update to the new position and a pointer is returned for the beginning of the allocated section:
Code:
allocp += n;
return allocp - n;
If there is NOT sufficient space to allocate
n bytes/char a NULL is returned instead:
When utilizing such a technique with a microcontroller, some thought should be given to the:
A 1000 bytes of RAM maybe too large is many cases, depending of the microcontroller in question, to dedicate solely to a character array.
BigDog