By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

confused about pointer arithmetic

P: n/a
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!

Feb 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ra********@gmail.com wrote:
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.

--
Ian Collins.
Feb 7 '06 #2

P: n/a

<ra********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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;
}

--
RSH


Feb 7 '06 #3

P: n/a
On 6 Feb 2006 21:14:35 -0800, ra********@gmail.com wrote:
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
Feb 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.