地址算术 [英] Address Arithmetic

查看:50
本文介绍了地址算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


如果我使用G ++ 4.0.2编译以下程序并运行它,我会得到

以下输出:


#include< iostream>

使用命名空间std;


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

int x,y;


cout<< 地址x = << (无符号长)& x<< endl;

cout<< y =的地址。 << (无符号长)& y<< endl;

cout<< 地址差异="

<< (无符号长)& y - (无符号长)& x<<结束;


返回1;

}


$ ./a.out

地址x = 3219613408

地址y = 3219613404

地址差异= 4294967292


我想知道为什么地址差异显示一个长的,看似

的随机数,而不是4?我错了吗?


谢谢,

-Andy

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

推荐答案

./ a.out

地址x = 3219613408

地址y = 3219613404

地址差异= 4294967292


我想知道为什么地址差异显示一个长的,看似

的随机数,而不是4?我错了吗?


谢谢,

-Andy

./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


Acck !在提交之前我应该​​更长时间地回顾一下我的问题

UseNet :-(。由于某种原因,y在地址

空间中的x之前分配。对无符号值进行算术运算产生下溢和大

值。


没关系。

-Andy


aist ... @ gmail.com写道:
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:

大家好,


如果我编译以下程序使用G ++ 4.0.2并运行它我得到

以下输出:


#include< iostream>

使用命名空间std;


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

int x,y;


cout<<""地址x ="<<(unsigned long)& x<< endl;

cout<<""地址y ="<<(unsigned long)& y<< endl;

cout<<""地址差异="

<<(unsigned lo ng)& y - (无符号长)& x<<结束;


返回1;

}

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

地址x = 3219613408

地址y = 3219613404

地址差异= 4294967292


我想知道为什么地址差异显示一个长的,看似

的随机数,而不是4?我错了吗?


谢谢,

-Andy
./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


这篇关于地址算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆