的reinterpret_cast<> [英] reinterpret_cast<>

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

问题描述



在SunOS 5.9上编写了这段代码,编译器g ++ 2.95.3


试图查看int或short的字节顺序int转换为

char *。不行。 char * cpt似乎没有初始化!!。

为什么会这样?

int main()

{

int x = 0x01020304;

int * ipt =& x;

cout<< ipt:<< hex<< ipt<<结束// ok ---

char * cpt = reinterpret_cast< char *>(ipt);

cout<< cpt: << hex<< cpt<< endl; //没有cpt的输出!!!

short int * spt = reinterpret_cast< short int *> (ipt);

cout<< spt: << hex<< spt<< endl; // ok ---


}


问候,

阿曼。

-

通过Mailgate.ORG服务器发布 - http:// www。 Mailgate.ORG


[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

解决方案



" Aman" <上午** @ techie.com>在留言中写道

新闻:40 ************************************ @mygate .mailgate.org ...


在SunOS 5.9上编写了这段代码,编译器g ++ 2.95.3
试图查看字节转换为
char *的int或short int的顺序。不行。 char * cpt似乎没有被初始化!!
为什么会这样?


它是。

int main()
{x /> int x = 0x01020304;
int * ipt =& x;
cout<< ipt:<< hex<< ipt<<结束// ok ---
char * cpt = reinterpret_cast< char *>(ipt);
cout<< cpt: << hex<< cpt<< endl; //没有输出为cpt !!!




类型''char *''的流插入器(<<)解释了参数

作为指向C风格字符串的指针(零终止字符数组)。

例如

char * p =" hello" ;;

cout<< p << \\\
; / *打印你好 * /


有两件事正在发生:


- 系统上的sizeof(int)是四个字节,在这种情况下没有

的值为零,产生''undefined behavior'',因为

cout<<将运行你的数组结束。


-sizeof(int)超过四个字节,在这种情况下,其中一些

的值为零,并且看起来在你的机器上,在任何非零字节之前存储了零

,在这种情况下,

cout<< cpt在任何输出之前终止。


尝试:


for(size_t i = 0; i< sizeof x; ++ i)

cout<< hex<< cpt [i];

cout<< ''\\ n';;

-Mike


" Mike Wahler" < MK ****** @ mkwahler.net>在消息中写道

news:x4 ***************** @ newsread1.news.pas.earthl ink.net ...


尝试:

for(size_t i = 0; i< sizeof x; ++ i)
cout<< hex<< cpt [i];




我们需要转换为int来获取

的文本表示字节'的值:


cout<< hex<< static_cast< int>(cpt [i]);


对不起的疏忽。


-Mike


>试图通过转换为

char *来查看int或short int的字节顺序




为什么不使用联合?


Hi,
wrote this piece of code on SunOS 5.9 , compiler g++ 2.95.3

trying to see the byte order of an int or short int by converting to
char* . doesn''t work . the char* cpt doesn''t seem to be initialized !!.
why would that be ?
int main()
{
int x=0x01020304 ;
int* ipt = &x ;
cout << "ipt : "<< hex << ipt << endl ; // ok ---
char* cpt = reinterpret_cast<char*>(ipt) ;
cout << "cpt : " << hex << cpt <<endl ; // NO output for cpt !!!
short int* spt = reinterpret_cast<short int*> (ipt) ;
cout << "spt : " << hex << spt <<endl ; // ok ---

}

regards,
Aman.
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

解决方案


"Aman" <am**@techie.com> wrote in message
news:40************************************@mygate .mailgate.org...

Hi,
wrote this piece of code on SunOS 5.9 , compiler g++ 2.95.3

trying to see the byte order of an int or short int by converting to
char* . doesn''t work . the char* cpt doesn''t seem to be initialized !!.
why would that be ?
It is.


int main()
{
int x=0x01020304 ;
int* ipt = &x ;
cout << "ipt : "<< hex << ipt << endl ; // ok ---
char* cpt = reinterpret_cast<char*>(ipt) ;
cout << "cpt : " << hex << cpt <<endl ; // NO output for cpt !!!



The stream inserter (<<) for type ''char*'' interprets the argument
as a pointer to a ''C-style'' string (zero terminated array of char).
e.g.
char *p = "hello";
cout << p << ''\n''; /* prints "hello" */

On of two things is happening:

- sizeof(int) on your system is four bytes, in which case none
of them has a value of zero, producing ''undefined behavior'', since
cout<< will run off the end of your array.

-sizeof(int) is more than four bytes, in which case some of them
have zero value, and it appears that on your machine, a zero
is stored before any of the nonzero bytes, in which case
cout << cpt terminates before any output.

Try:

for(size_t i = 0; i < sizeof x; ++i)
cout << hex << cpt[i];
cout << ''\n'';
-Mike


"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:x4*****************@newsread1.news.pas.earthl ink.net...


Try:

for(size_t i = 0; i < sizeof x; ++i)
cout << hex << cpt[i];



We need to cast to int to get the textual representation of
the byte''s value:

cout << hex << static_cast<int>(cpt[i]);

Sorry for the oversight.

-Mike


> trying to see the byte order of an int or short int by converting to

char*



Why not use a union?


这篇关于的reinterpret_cast&LT;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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