473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

confused about pointer arithmetic

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
3 2177
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

<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ceo | last post by:
Hi, Following is a program that doesn't give the expected output, not sure what's wrong here. I'm adding the size of derived class to the base class pointer to access the next element in the...
22
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do...
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
7
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
3
by: =?Utf-8?B?RGlwZXNoX1NoYXJtYQ==?= | last post by:
Hi all, I am porting my code in VC++ to VC.net i.e managed. I have seen somewhere that i need to convert my char* to String*, but String * doesnt perform pointer arithmetic like String* p; *p++='...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
25
by: Ioannis Vranos | last post by:
Are the following codes guaranteed to work always? 1. #include <iostream> inline void some_func(int *p, const std::size_t SIZE) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.