473,466 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Address Arithmetic

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

Jul 6 '06 #1
14 1449
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

aist...@gmail.com 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
Jul 6 '06 #2
ai*****@gmail.com 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.
Jul 6 '06 #3

<ai*****@gmail.comskrev i en meddelelse
news:11**********************@m73g2000cwd.googlegr 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
Jul 6 '06 #4
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
Jul 6 '06 #5
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::ptrdiff_t

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


std::cout << ...
--

Frederick Gotham
Jul 6 '06 #6
<ai*****@gmail.comwrote:
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 - 8) = (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/
Jul 7 '06 #7
On Thu, 06 Jul 2006 23:40:43 GMT, Frederick Gotham
<fg*******@SPAM.comwrote 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::ptrdiff_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
Jul 7 '06 #8
On Thu, 06 Jul 2006 23:37:40 GMT, Frederick Gotham
<fg*******@SPAM.comwrote 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
Jul 7 '06 #9
In article <7j********************************@4ax.com>,
ja*******@spamcop.net 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.
Jul 8 '06 #10
* Jerry Coffin:
In article <7j********************************@4ax.com>,
ja*******@spamcop.net 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.
Not sure about that and right now I don't feel like checking it out;
however, I'm writing to point out that the /standard library/ provides
functions to compare unrelated pointers safely.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 8 '06 #11
Alf P. Steinbach wrote:
>
Not sure about that and right now I don't feel like checking it out;
however, I'm writing to point out that the /standard library/ provides
functions to compare unrelated pointers safely.
Really?

The original poster was quite right. The line from the standard
explicitly defines unspecified result:

If two pointers p and q of the same type point to different objects
that are not members of the same object or elements of the same array
or to different functions, or if only one of them is null, the
results of p<q, p>q, p<=q, and p>=q are unspecified.
I don't know what you mean by the quoted passage above. But I don't
believe there is anything in the library that provides any stricter
ordering on unrelated pointers that my passage above (from 5.9 of the
standard).
Jul 8 '06 #12
* Ron Natalie:
Alf P. Steinbach wrote:
>>
Not sure about that and right now I don't feel like checking it out;
however, I'm writing to point out that the /standard library/ provides
functions to compare unrelated pointers safely.

Really?
Yep.

The original poster was quite right. The line from the standard
explicitly defines unspecified result:

If two pointers p and q of the same type point to different objects
that are not members of the same object or elements of the same array
or to different functions, or if only one of them is null, the
results of p<q, p>q, p<=q, and p>=q are unspecified.
That means /unspecified/, not undefined, as Jack wrote. So Jerry was
right, and Jack was wrong; what the OP was, I don't know. Thanks for
doing what I didn't have time to do (looking this up in the standard).

I don't know what you mean by the quoted passage above. But I don't
believe there is anything in the library that provides any stricter
ordering on unrelated pointers that my passage above (from 5.9 of the
standard).
std::less & family.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 8 '06 #13
Alf P. Steinbach wrote:
>
That means /unspecified/, not undefined, as Jack wrote. So Jerry was
right, and Jack was wrong; what the OP was, I don't know. Thanks for
doing what I didn't have time to do (looking this up in the standard).
Your followup was to Jerry Coffin's correction to Jack (which did say
unspecified) ...that's what was confusing.

You're right about the <functionalrelational templates. I'd forgotten
that. It's one of the places where less() is something more than a pure
wrapper around the < operator.
Jul 8 '06 #14
Jack Klein wrote:
Frederick Gotham wroteL
>> 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.
But they do point to parts of the same object: the array 'obj'.

Jul 10 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shama.bell | last post by:
Hello, I am a newbie and i have problems filling an array with the mac address read from the command line. The mac address is FF:FF:FF:FF:FF:FF options, args = parser.parse_args() #...
2
by: first10 | last post by:
Hello, Apologies for being slightly off topic - but this group seems the best fit for the question. How does the compiler handle the problem of not knowing which address a DLL will be loaded...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
24
by: Frederick Gotham | last post by:
There is a thread currently active on this newsgroup entitled: "how to calculate the difference between 2 addresses ?" The thread deals with calculating the distance, in bytes, between two...
11
by: !truth | last post by:
Hi, i feel confused about the following program, and it's works to get a pointer-member's address. #define NETDEV_ALIGN 32 #define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1) ...
16
by: somenath | last post by:
Hi All. I would like to know the following information. 1)Is there any difference between the address and integer ? For example suppose int x = 500; And int y =10; Suppose address of y is...
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
15
by: vjay | last post by:
Check the code below guys void main() { char a = {1,2,3,4,5}; char *ptr; ptr = (char*)(&a + 1); printf("%d",*(ptr-1)); } The output of the program is 5
37
by: Richard Heathfield | last post by:
candide said: They aren't. An array is an array. An address is a pointer value. These are not the same thing. If you mean that &array and &array are the same, they aren't. They have different...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.