Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Address Arithmetic

Reply
Thread Tools

Address Arithmetic

 
 
aistone@gmail.com
Guest
Posts: n/a
 
      07-06-2006
Hi all,

If I compile the following program with G++ 4.0.2 and run it I get the
following output:

#include <iostream>
using namespace std;

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

cout << "Address of x = " << (unsigned long)&x << endl;
cout << "Address of y = " << (unsigned long)&y << endl;
cout << "Difference in addresses = "
<< (unsigned long)&y - (unsigned long)&x << endl;

return 1;
}

$ ./a.out
Address of x = 3219613408
Address of y = 3219613404
Difference in addresses = 4294967292

I'm wondering why does "Difference in addresses" show a long, seemingly
random, number instead of 4? Am I casting things wrong?

Thanks,
-Andy

 
Reply With Quote
 
 
 
 
aistone@gmail.com
Guest
Posts: n/a
 
      07-06-2006
Acck! I should review my problems longer before submitting them to
UseNet . For some reason y is allocated before x in the address
space. Doing arithmatic on the unsigned values underflows and a "big"
value is produced.

Never mind.
-Andy

(E-Mail Removed) wrote:
> Hi all,
>
> If I compile the following program with G++ 4.0.2 and run it I get the
> following output:
>
> #include <iostream>
> using namespace std;
>
> int main(char argc, char *argv[]) {
> int x, y;
>
> cout << "Address of x = " << (unsigned long)&x << endl;
> cout << "Address of y = " << (unsigned long)&y << endl;
> cout << "Difference in addresses = "
> << (unsigned long)&y - (unsigned long)&x << endl;
>
> return 1;
> }
>
> $ ./a.out
> Address of x = 3219613408
> Address of y = 3219613404
> Difference in addresses = 4294967292
>
> I'm wondering why does "Difference in addresses" show a long, seemingly
> random, number instead of 4? Am I casting things wrong?
>
> Thanks,
> -Andy


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Acck! I should review my problems longer before submitting them to
> UseNet . For some reason y is allocated before x in the address
> space. Doing arithmatic on the unsigned values underflows and a "big"
> value is produced.
>

Framing a post to Usenet is often a good way to solve you own problems!

--
Ian Collins.
 
Reply With Quote
 
Eric Jensen
Guest
Posts: n/a
 
      07-06-2006

<(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed) oups.com...
> Hi all,
>
> If I compile the following program with G++ 4.0.2 and run it I get the
> following output:
>
> #include <iostream>
> using namespace std;
>
> int main(char argc, char *argv[]) {
> int x, y;
>
> cout << "Address of x = " << (unsigned long)&x << endl;
> cout << "Address of y = " << (unsigned long)&y << endl;
> cout << "Difference in addresses = "
> << (unsigned long)&y - (unsigned long)&x << endl;
>
> return 1;
> }
>
> $ ./a.out
> Address of x = 3219613408
> Address of y = 3219613404
> Difference in addresses = 4294967292
>
> I'm wondering why does "Difference in addresses" show a long, seemingly
> random, number instead of 4? Am I casting things wrong?


Because you assume that the address of pointer y is greater than pointer x.

On the win32 platform your cast will result in pointer turncation, most
likely also on linux.
You need to cast to a 64 bit number instead of a 32 bit number.

If you change your cast to (unsigned long long) it will work fine on atleast
win32.

int x, y;
unsigned long long addrOfX = reinterpret_cast<unsigned long long>(&x),
addrOfY = reinterpret_cast<unsigned long
long>(&y);
cout << "Address of x = " << addrOfX << endl <<
"Address of y = " << addrOfY << endl <<
"Difference in addresses = " <<
(addrOfX < addrOfY ? (addrOfY - addrOfX) : (addrOfX - addrOfY))
<< endl;

//eric


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      07-06-2006
posted:


> I'm wondering why does "Difference in addresses" show a long, seemingly
> random, number instead of 4? Am I casting things wrong?



Perhaps you want something like:

#include <stdio.h>
#include <stddef.h>

typedef int SomeType;

int main(void)
{
SomeType const obj[2];

ptrdiff_t const diff = (char*)(obj + 1) - (char*)obj;

printf("The difference is %lu bytes.\n", diff);
}

(Would "ptrdiff_t" be preferable over "size_t"? Is "%lu" suitable to use
with "printf"?)


When you define two objects of the same type as follows:

int a, b;

You have no guarantee that there is no padding between them, or even that
they're stored ANYWHERE near each other in memory.



--

Frederick Gotham
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      07-06-2006
Frederick Gotham posted:


I forgot I was on the C++ forum, rather than C.


> #include <stdio.h>
> #include <stddef.h>



#include <iostream>
#include <cstddef>


> typedef int SomeType;
>
> int main(void)
> {
> SomeType const obj[2];
>
> ptrdiff_t const diff = (char*)(obj + 1) - (char*)obj;



std:trdiff_t


> printf("The difference is %lu bytes.\n", diff);




std::cout << ...


--

Frederick Gotham
 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      07-07-2006
<(E-Mail Removed)> wrote:

> cout << "Difference in addresses = "
> << (unsigned long)&y - (unsigned long)&x << endl;
>
> Address of x = 3219613408
> Address of y = 3219613404
> Difference in addresses = 4294967292
>
> I'm wondering why does "Difference in addresses" show a long,
> seemingly random, number instead of 4?



(uint32)(4 - = (uint32) (-4)
= 2^32 - 4
= 4294967296 - 4
= 4294967292

Which is exactly what you got.


--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-07-2006
On Thu, 06 Jul 2006 23:40:43 GMT, Frederick Gotham
<(E-Mail Removed)> wrote in comp.lang.c++:

> Frederick Gotham posted:
>
>
> I forgot I was on the C++ forum, rather than C.
>
>
> > #include <stdio.h>
> > #include <stddef.h>

>
>
> #include <iostream>
> #include <cstddef>
>
>
> > typedef int SomeType;
> >
> > int main(void)
> > {
> > SomeType const obj[2];
> >
> > ptrdiff_t const diff = (char*)(obj + 1) - (char*)obj;


Subtracting pointers that do not point to parts of the same object is
undefined behavior.

>
>
> std:trdiff_t
>
>
> > printf("The difference is %lu bytes.\n", diff);

>
>
>
> std::cout << ...


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-07-2006
On Thu, 06 Jul 2006 23:37:40 GMT, Frederick Gotham
<(E-Mail Removed)> wrote in comp.lang.c++:

> posted:
>
>
> > I'm wondering why does "Difference in addresses" show a long, seemingly
> > random, number instead of 4? Am I casting things wrong?

>
>
> Perhaps you want something like:
>
> #include <stdio.h>
> #include <stddef.h>
>
> typedef int SomeType;
>
> int main(void)
> {
> SomeType const obj[2];
>
> ptrdiff_t const diff = (char*)(obj + 1) - (char*)obj;


Subtracting or comparing pointers that do not point to parts of the
same object is undefined behavior.

> printf("The difference is %lu bytes.\n", diff);
> }
>
> (Would "ptrdiff_t" be preferable over "size_t"? Is "%lu" suitable to use
> with "printf"?)


ptrdiff_t is a signed type. There is no guarantee whatsoever what, if
any, relationship it might have with an unsigned long, so "%lu" is
particularly inappropriate. In fact, it needs a cast to whatever type
of printf() conversion specified is used.

> When you define two objects of the same type as follows:
>
> int a, b;
>
> You have no guarantee that there is no padding between them, or even that
> they're stored ANYWHERE near each other in memory.


That's why subtracting or comparing their addresses is strictly
undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      07-08-2006
In article <(E-Mail Removed)>,
(E-Mail Removed) says...

[ ... ]

> Subtracting or comparing pointers that do not point to parts of the
> same object is undefined behavior.


Close, but not quite right. The behavior is defined, but the result
is unspecified. If you do something like this:

int a, b;

if ( a < b)
std::cout << "A less than B";
else
std::cout << "A greater than or equal to B";

It's entirely open to question _which_ leg of the if statement will
execute, but you basically get normal execution: one leg or the other
will execute, and things proceed normally from there. Undefined
behavior would mean absolutely _anything_ could happen from there
out.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Byte Address Arithmetic Debate Frederick Gotham C++ 24 11-21-2006 01:28 PM
Address of array behavior in pointer arithmetic joshc C Programming 16 06-15-2006 10:14 PM
A kind of arithmetic for subnetting and ip address management lily Cisco 0 05-08-2006 09:32 AM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Arithmetic on function address Stephen Biggs C Programming 21 04-28-2004 11:17 AM



Advertisments