5

Going through K&R I too a look at the following code:

#define ALLOCSIZE 1000
static char allocbuf[MAXLINE];
static char *allocp = allocbuf

char *alloc(int n){
       if (allocbuf+ALLOCSIZE-allocp>=n){
               allocp+=n;
               return allocp-n;
       }
       else { ... }

I'm afraid my question is very simple, but I can't get my head round the "if" line. What value is allocbuf taking? It is a char array, right? I looked back at the array stuff in the book, but it didn't help. allocp initially points to the zeroth element of the array, right?

4
  • what is ALLOCSIZE? also mind that allocbuf is a constant (the address of the array) so it cannot take any other value. Commented Jul 6, 2011 at 14:29
  • I think there is something missing in the code Commented Jul 6, 2011 at 14:31
  • Sorry, I wrote the #define line wrong... editing Commented Jul 6, 2011 at 14:34
  • Syntax error at line five (I expected a semicolon or an operator). Is this the real code? Commented Jul 6, 2011 at 14:38

4 Answers 4

2

allocbuf is an array of type char [], but in many contexts the identifier itself decays to a pointer of type char *, holding the starting address of the array. Note that this doesn't mean that allocbuf is a pointer, it is still an array.

So, the condition of the if statement performs some pointer arithmetic.

Sign up to request clarification or add additional context in comments.

4 Comments

But shouldn't The if-statement be: if(pntr+ALLOCSIZE-allocp>=n), where char *pntr = &allocbuf ?
You are absolutely right about this, this is the correct way in principle. It's just that C happens to have a shortcut syntax that results in allocbuf being the same as &allocbuf in this context.
Thank you. Also, what other "contexts" can we do this in? Any arithmetic operations will be implied by the compiler to mean the pointer to the array?
1

While they're declared in different ways, allocp and allocbuf are both char arrays (char*) and allocp effectively points on the first char of the buffer after initializing, and after getting through the "if" body, to the same adress + the number of bytes allocated, and this number increases with each new cycle in the "if" body. To sum up, it points on the first free char in the buffer. The "if" line you're stuck with is aimed to verify if there's enough place for allocating n chars in the allocbuf, the static buffer. This line could be discomposed as follows :

char* static_buffer_beginning = allocbuf;
char* static_buffer_ending = static_buffer_beginning + MAXLINE;
int   nb_chars_still_available = static_buffer_ending - allocp;
if (nb_chars_still_available >= n) {

I'm just a little confused by the "ALLOCSIZE" which appears in your code : what's his value, where does it come from?! I assume it's a typo or something like that and that its value is equal to MAXLINE, but would like to be sure for not to give you a wrong answer.

3 Comments

Ignore MAXLINE. I absent mindedly typed it in, when I meant ALLOCBUF. I edited the question.
Okay. Then replace MAXLINE by ALLOCSIZE, or what macro you use, and the explanation stays the same.
Hmmm, quite new to stackOverflow, so I don't know how to comment an answer which is not from me, but I absolutely don't agree with your first comment on @Blagovest Buyukliev answer. char *pntr = allocbuf; is OK, but char *pntr = &allocbuf; is wrong, gcc will yield about incompatible pointer types.
0

Think of allocbuf as the pointer to the beginning of your RAM, say 0. Then allocbuf+ALLOCSIZE will be pointing to the end of your RAM. allocp is pointing to the end of the allocated region, somewhere in the middle of your RAM. So allocbuf+ALLOCSIZE-allocp will give you the free memory size. The if statement checks if your requested allocation size (n) is less than the free memory available.

1 Comment

I understand the arithmetic. The book has a nice picture to help explain it. It's the use of an array name as a pointer to the array. I can't get my head around it.
0

allocbuf is a static array, actually it points to first element of contiguous set of chars (the array). allocp is another pointer to the contiguous array and you can change its value to point to the array elements.

2 Comments

allocbuf is a static array, actually it points to first element - an array doesn't point to anything, it's just that the identifier decays to a pointer.
Thankyou. The problem I have is in your first sentence. "allocbuf is a static array... it points to...". If it isn't a pointer how can it point?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.