Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsure about correctness of a sentence from K&R - pointer arithmetic | Freeing procedure

Quotation:

The test if (allocbuf + ALLOCSIZE - allocp >= n) { checks if there's enough room to satisfy a request for n characters. If there is, the new value of allocp would be at most one beyond the end of allocbuf.

The code which it relates to:

#define ALLOCSIZE 10000 /* size of available space */
static char allocbuf[ALLOCSIZE]; /* storage for alloc */
static char *allocp = allocbuf; /* 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;
}

So how can it be beyond the last position in allocbuf? Which in my opinion is allocbuf[9999]

Anything beyond that eg. allocbuf[10000] is incorrect and is a memory leak, am I right?


The second part of the question - I though that afree function according to its name is deleting the value saved at particular places in an array. However as I can see it just moves the "recording head" just a few places to the left of an array? Data saved there remains untouched.

like image 792
Peter Cerba Avatar asked Aug 14 '12 14:08

Peter Cerba


People also ask

What are the 3 types of conscience?

Certain conscience means convinced without any doubt that an action is good or bad. Doubtful conscience means when you cannot choose between good and bad choices. Lax conscience means when you see no sin where there actually is sin.

What is perplexed conscience?

Conscience may be perplexed, being compelled to choose between two evils. But observe the ambiguity in our proposition. If the "evils" be sins, you are not allowed to choose either of them; both must be rejected.

What is scrupulous conscience example?

Scrupulous Conscience is when a person decides that his/her action is immoral on the basis of weak or insufficient reasoning. An example of scrupulous consciences is when a person at the grocery gave P100 to the cashier for a P88 worth of purchase grocery supplies.


1 Answers

allocp should always point to the last free memory position but when there is no memory positions free it will be one beyond the end of allocbuf.

Consider the situation when there is only one single memory cell left in the buffer: allocp will point to allocbuffer[9999] since that is the last free memory cell. Now when you make the function call alloc(1) the test

allocbuf + ALLOCSIZE - allocp >= n

will return true as it should since you are trying to allocate one char and there is exactly one char left. Then the very last memory position will be allocated. Now allocbuf - allocp == ALLOCSIZE and is one beyond the end of allocbuf. But in that case the above mentioned test will always return false and thus no memory will ever be accessed beyond the scope of allocbuf.


About your question regarding afree: The initial value of the memory returned by malloc is undefined. This means that you can never make any assumptions about it and must overwrite it before use. Therefore afree does not have to delete any data contrary to what you're assuming. It is perfectly fine for it to simply mark it as no longer in use and available for future allocation.

On a side note there is a function very similar to malloc named calloc which after allocating the requested memory block initializes it all to zero.

like image 184
paldepind Avatar answered Oct 02 '22 17:10

paldepind