473,322 Members | 1,398 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,322 software developers and data experts.

pointer arithmetic

a
I am having trouble understanding the results of the following code:

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{

unsigned short *IO = reinterpret_cast<unsigned short*>(0x8000);
unsigned short offset = static_cast<unsigned short>(0x2);
unsigned short *value = reinterpret_cast<unsigned short*>(IO+offset);

cout << "IO is " << hex << IO <<endl;
cout << "value is " << hex << value <<endl;

return 0;
}

The output for this case is: IO is 00008000, value is 00008004
I had expected value to be 00008002

If IO is not a pointer, I get the result I expected.

What is going on here?

Thanks to anyone with a helpful explanation.
Jul 19 '05 #1
6 6875
a wrote:
I am having trouble understanding the results of the following code:

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{

unsigned short *IO = reinterpret_cast<unsigned short*>(0x8000);
This is really a bad idea...
unsigned short offset = static_cast<unsigned short>(0x2);
The cast in entirely unnecessary here. Never cast unless you absolutely
have to.
unsigned short *value = reinterpret_cast<unsigned short*>(IO+offset);
The cast is also unnecessary here.

cout << "IO is " << hex << IO <<endl;
cout << "value is " << hex << value <<endl;

return 0;
}

The output for this case is: IO is 00008000, value is 00008004
I had expected value to be 00008002
I get the same (after removing the unnecessary casts).

unsigned int is frequently 2 bytes on modern implementations. Because of
that, given a pointer to an unsigned int, adding 1 to that pointer has
to move it up 2 bytes to get to the next unsigned int. Likewise, adding
2 moves it up 4 bytes.

Basically, pointer arithmetic always works by translating:

p + n

(where p is a pointer and n is an integer) into:

p + n*sizeof(*p)

Of course, the second expression literally means something different to
the C++ language, but this is just a demonstration. The second is kind
of the compiler's interpretation. In your case, this translates to:

p + n*sizeof(unsigned int)

or:

p + n*2

If IO is not a pointer, I get the result I expected.

What is going on here?


Pointer arithmetic.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
a wrote:
I am having trouble understanding the results of the following code:

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{

unsigned short *IO = reinterpret_cast<unsigned short*>(0x8000);
unsigned short offset = static_cast<unsigned short>(0x2);
// no cast needed here unsigned short *value = reinterpret_cast<unsigned short*>(IO+offset);
cout << "IO is " << hex << IO <<endl;
cout << "value is " << hex << value <<endl;

return 0;
}

The output for this case is: IO is 00008000, value is 00008004
I had expected value to be 00008002

If IO is not a pointer, I get the result I expected.

What is going on here?

Thanks to anyone with a helpful explanation.

unsigned short *p = something;
unsigned short a;

// these two expressions are equivalent
a = p[5];
a = *(p+5);

The point is that when doing pointer arithmetic, the value that gets
added to the pointer value is multiplied by the size of the type pointed
to by the pointer. If it was different, the programmer would have to
worry about multiplying the offset by proper size and it would be really
inconvenient. Assuming that size of pointer == size of int the following
statement would be equivalent to the previous two:

int i = reinterpret_cast<int>(p) + 5*sizeof(unsigned short);
a = *reinterpret_cast<unsigned short*>(i);

--
Ahti Legonkov

Jul 19 '05 #3
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<mi*****************@newsread3.news.pas.earth link.net>...
a wrote:
I am having trouble understanding the results of the following code:

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{

unsigned short *IO = reinterpret_cast<unsigned short*>(0x8000);


This is really a bad idea...
unsigned short offset = static_cast<unsigned short>(0x2);


The cast in entirely unnecessary here. Never cast unless you absolutely
have to.
unsigned short *value = reinterpret_cast<unsigned short*>(IO+offset);


The cast is also unnecessary here.

cout << "IO is " << hex << IO <<endl;
cout << "value is " << hex << value <<endl;

return 0;
}

The output for this case is: IO is 00008000, value is 00008004
I had expected value to be 00008002


I get the same (after removing the unnecessary casts).

unsigned int is frequently 2 bytes on modern implementations. Because of
that, given a pointer to an unsigned int, adding 1 to that pointer has
to move it up 2 bytes to get to the next unsigned int. Likewise, adding
2 moves it up 4 bytes.


Presumably, wherever you've written "unsigned int" (here and later)
you meant "unsigned short". Just in case the OP was confused.

<snip>

GJD
Jul 19 '05 #4

"Ahti Legonkov" <le**@127.0.0.1> wrote in message news:3f**********@news.estpak.ee...
a wrote: The point is that when doing pointer arithmetic, the value that gets
added to the pointer value is multiplied by the size of the type pointed
to by the pointer.


Actually, it adds whatever it has to be to advance by that number of
objects. On word addressed machines adding 1 to a int pointer
literally adds 1 to the the internal value. On byte addressed machines
it does add sizeeof(int).
Jul 19 '05 #5
Gavin Deane wrote:
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<mi*****************@newsread3.news.pas.earth link.net>...

unsigned int is frequently 2 bytes on modern implementations. Because of
that, given a pointer to an unsigned int, adding 1 to that pointer has
to move it up 2 bytes to get to the next unsigned int. Likewise, adding
2 moves it up 4 bytes.

Presumably, wherever you've written "unsigned int" (here and later)
you meant "unsigned short". Just in case the OP was confused.


Yeah... Check the posting time for an explanation. ;)

Yes, every occurrence of 'unsigned int' should have been 'unsigned short'.

-Kevin (shouldn't post after 2 a.m.)
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6
a

"a" <ag****@cox.net> wrote in message
news:QPV7b.48887$Qy4.7224@fed1read05...
I am having trouble understanding the results of the following code:

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{

unsigned short *IO = reinterpret_cast<unsigned short*>(0x8000);
unsigned short offset = static_cast<unsigned short>(0x2);
unsigned short *value = reinterpret_cast<unsigned short*>(IO+offset);

cout << "IO is " << hex << IO <<endl;
cout << "value is " << hex << value <<endl;

return 0;
}

The output for this case is: IO is 00008000, value is 00008004
I had expected value to be 00008002

If IO is not a pointer, I get the result I expected.

What is going on here?

Thanks to anyone with a helpful explanation.


Thanks to all for a nice explanation.

I do realize the casts were unneccesary (except the first, and it should be
noted that this is a piece of test code to a fixed address that will be
replaced with an OS function).
Jul 19 '05 #7

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; }
3
by: randomtalk | last post by:
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...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
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...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.