confused about pointer arithmetic

R

randomtalk

hello everyone!

Well, recently i've been trying to pick up c and see what is pointer
all about (been programming in lisp/python for the better part of my
last two years)..

mmm.. I'm currently reading The C Programming Language Second Edition..
When i hit on pointer arithmetic example, i have no idea what's
happening, hopefully some of you could alleviate my confusion, so here
goes:

Here is a few lines to illustrate my confusion:
#include <stdio.h>
int main(void)
{
char allocbuf[10000];
char *allocp = &allocbuf[3000];
printf("allocbuf is: %p, \n", allocbuf);
printf("allocp is: %p, \n", allocp);
printf("after addition, result is: %i \n", allocbuf+10000);
printf("after substracttion, result is: %i \n",
allocbuf+10000-allocp);
return 0;
}

Basically, in the book, they are trying to implment a trivial version
of malloc and free. First of all, allocate a size of the buffer, called
allocbuf, then *allocp points to next empty spot (here i just used 3000
for arguments sake), then for some reason, allocbuf + 10000 - allocp is
suppose to give you how much space is left (and it does, returns
7000).. However, conceptually (most likely it's mostly wrong), when an
10000 block array add 10000, and minus the used space, isn't suppose to
give you the free space.. That's where i got confused, also, neither
the printed result by either allocbuf, or allocbuf+10000 helped me..

So if any kind soul would help me, it would be greatly appreciated :)

Thanks!
 
I

Ian Collins

hello everyone!

Well, recently i've been trying to pick up c and see what is pointer
all about (been programming in lisp/python for the better part of my
last two years)..

mmm.. I'm currently reading The C Programming Language Second Edition..
When i hit on pointer arithmetic example, i have no idea what's
happening, hopefully some of you could alleviate my confusion, so here
goes:

Here is a few lines to illustrate my confusion:
#include <stdio.h>
int main(void)
{
char allocbuf[10000];
char *allocp = &allocbuf[3000];
printf("allocbuf is: %p, \n", allocbuf);
printf("allocp is: %p, \n", allocp);
printf("after addition, result is: %i \n", allocbuf+10000);
printf("after substracttion, result is: %i \n",
allocbuf+10000-allocp);
return 0;
}

Basically, in the book, they are trying to implment a trivial version
of malloc and free. First of all, allocate a size of the buffer, called
allocbuf, then *allocp points to next empty spot (here i just used 3000
for arguments sake), then for some reason, allocbuf + 10000 - allocp is
suppose to give you how much space is left (and it does, returns
7000).. However, conceptually (most likely it's mostly wrong), when an
10000 block array add 10000, and minus the used space, isn't suppose to
give you the free space.. That's where i got confused, also, neither
the printed result by either allocbuf, or allocbuf+10000 helped me..

So if any kind soul would help me, it would be greatly appreciated :)
Assume for argument's sake allocbuf = 0, so allocp = 3000.

0+10000-3000 = 7000 - the free space.
 
R

Robin Haigh

mmm.. I'm currently reading The C Programming Language Second Edition..
When i hit on pointer arithmetic example, i have no idea what's
happening, hopefully some of you could alleviate my confusion, so here
goes:

Here is a few lines to illustrate my confusion:
#include <stdio.h>
int main(void)
{
char allocbuf[10000];
char *allocp = &allocbuf[3000];
printf("allocbuf is: %p, \n", allocbuf);
printf("allocp is: %p, \n", allocp);
printf("after addition, result is: %i \n", allocbuf+10000);
printf("after substracttion, result is: %i \n",
allocbuf+10000-allocp);
return 0;
}

Basically, in the book, they are trying to implment a trivial version
of malloc and free. First of all, allocate a size of the buffer, called
allocbuf, then *allocp points to next empty spot (here i just used 3000
for arguments sake), then for some reason, allocbuf + 10000 - allocp is
suppose to give you how much space is left (and it does, returns
7000).. However, conceptually (most likely it's mostly wrong), when an
10000 block array add 10000, and minus the used space, isn't suppose to
give you the free space.. That's where i got confused, also, neither
the printed result by either allocbuf, or allocbuf+10000 helped me..


In most situations the name of an array represents the start address. So
allocbuf+10000 is the end address (the byte after the last byte of
allocbuf).

In your code you're displaying 2 pointers with %p and the third with %i.
Strictly they should all be done with %p, but if they're all shown in hex,
the arithmetic won't be very clear.

Since this isn't real code, it might be more helpful (though not strictly
conforming) to cheat and convert everything to unsigned long (as we did
before %p was invented)

#include <stdio.h>
int main(void)
{
char allocbuf[10000];
char *allocp = &allocbuf[3000];
printf("allocbuf is: %lu, \n", (unsigned long)allocbuf);
printf("allocp is: %lu, \n", (unsigned long)allocp);
printf("after addition, result is: %lu \n", (unsigned
long)(allocbuf+10000));
printf("after substracttion, result is: %lu \n",
(unsigned long)(allocbuf+10000-allocp));
return 0;
}
 
B

Barry Schwarz

hello everyone!

Well, recently i've been trying to pick up c and see what is pointer
all about (been programming in lisp/python for the better part of my
last two years)..

mmm.. I'm currently reading The C Programming Language Second Edition..
When i hit on pointer arithmetic example, i have no idea what's
happening, hopefully some of you could alleviate my confusion, so here
goes:

Here is a few lines to illustrate my confusion:
#include <stdio.h>
int main(void)
{
char allocbuf[10000];
char *allocp = &allocbuf[3000];
printf("allocbuf is: %p, \n", allocbuf);

%p requires the corresponding argument to be a void*. This will work
because the standard requires void* and char* to have the same
representation but it is a good idea to always cast the corresponding
argument to void*:
printf("allocbuf is: %p, \n", (void*)allocbuf);
printf("allocp is: %p, \n", allocp);
printf("after addition, result is: %i \n", allocbuf+10000);

The result pointer/integer arithmetic is still a pointer. %i is not
suitable for printing pointer values. Use %p.
printf("after substracttion, result is: %i \n",
allocbuf+10000-allocp);
return 0;
}

Basically, in the book, they are trying to implment a trivial version
of malloc and free. First of all, allocate a size of the buffer, called
allocbuf, then *allocp points to next empty spot (here i just used 3000
for arguments sake), then for some reason, allocbuf + 10000 - allocp is
suppose to give you how much space is left (and it does, returns
7000).. However, conceptually (most likely it's mostly wrong), when an
10000 block array add 10000, and minus the used space, isn't suppose to
give you the free space.. That's where i got confused, also, neither
the printed result by either allocbuf, or allocbuf+10000 helped me..

The result of pointer/pointer subtraction is an integer (ptrdiff_t in
C99). The arithmetic expression is equivalent to
10000 - (allocp - allocbuf)

allocp is the address of the first unused byte in the buffer. allocbuf
is the address of the first byte in the buffer. The difference
between the two is the number of used bytes. 10000 is the total size
of the buffer. Total - used = unused.
So if any kind soul would help me, it would be greatly appreciated :)


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top