 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Aman Guest
|
Posted: Mon Feb 23, 2004 7:46 pm Post subject: reinterpret_cast<> |
|
|
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
cout << "cpt : " << hex << cpt <
short int* spt = reinterpret_cast
cout << "spt : " << hex << spt <
}
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! ]
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Tue Feb 24, 2004 3:25 am Post subject: Re: reinterpret_cast<> |
|
|
Aman wrote in news:4069be0f43c5d3619e70e2c68cc331c1.26421
@mygate.mailgate.org:
| Quote: | 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 ---
|
The above should output the same as if you wrote:
cout << &x << endl;
i.e. the address of x, however that is printed.
| Quote: | | char* cpt = reinterpret_cast
cout << "cpt : " << hex << cpt <
|
cout << "cpt : " << hex << unsigned(*cpt) << endl;
| Quote: | | short int* spt = reinterpret_cast
cout << "spt : " << hex << spt <
|
As for ipt above.
#include
int main()
{
using namespace std;
char *cpt = "test-string";
cout << cpt << endl;
}
should output test-string. i.e. the streams output a "string" for
char *'s and a memory address for other pointers.
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Applin Guest
|
Posted: Tue Feb 24, 2004 1:31 pm Post subject: Re: reinterpret_cast<> |
|
|
In comp.lang.c++.moderated Aman <aman (AT) techie (DOT) com> wrote:
| Quote: | 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
cout << "cpt : " << hex << cpt <
|
A char * is different from an int * in a crucial way: when you send a
char * to an output stream, C++ doesn't print the pointer, C++ prints
the CHARACTERS that the pointer points to!
cout << "Hello theren";
That was a const char *, but you wouldn't expect hex, would you?
For this non-portable program, cast your char * to another pointer type:
cout << "cpt : " << hex << (void *) cpt <
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Samuel Klatchko Guest
|
Posted: Tue Feb 24, 2004 1:32 pm Post subject: Re: reinterpret_cast<> |
|
|
"Aman" <aman (AT) techie (DOT) com> wrote
| Quote: | int main()
{
int x=0x01020304 ;
int* ipt = &x ;
cout << "ipt : "<< hex << ipt << endl ; // ok ---
char* cpt = reinterpret_cast
cout << "cpt : " << hex << cpt <
}
|
The iostream code assumes that a char* is a pointer to a string that
is ' ' terminated. So iostreams is dereferencing the pointer and
printing out each character until it finds a ' '.
Most likely, when that bit pattern is turned into a pointer and
dereferenced, there is a ' ' byte in that place in memory.
You're lucky the code ran and did nothing instead of crashing when it
tried to dereference a random piece of memory.
samuel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Balog Pal Guest
|
Posted: Tue Feb 24, 2004 1:36 pm Post subject: Re: reinterpret_cast<> |
|
|
"Aman" <aman (AT) techie (DOT) com> wrote
| Quote: |
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
cout << "cpt : " << hex << cpt <
short int* spt = reinterpret_cast
cout << "spt : " << hex << spt <
}
|
Better try output wia printf
You're most likely tricked by the streams, char* is not threated as a
pointer but as a string, and is output as such. Or redirect the output to
a file and hexdump it. Or change your x init to 0x40414243
Paul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Aman Angrish Guest
|
Posted: Tue Feb 24, 2004 7:29 pm Post subject: Re: reinterpret_cast<> |
|
|
| Quote: | Technically, you're right, in that a union would involve undefined
behavior, where you are allowed to cast an arbitrary pointer to char* or
to unsigned char* and access the memory as an array of bytes. But a
union will work equally well in practice.
|
that is correct . I initially tried with the union too , but got
no output because of the same reason (char* interpretation by cout) .
thanks !!
Aman.
| Quote: | And of course, given his code, "equally well" means not at all. Which
was the purpose of his posting. Whether you use a union, or a char*
which you dereference, you must cast to int to avoid treating the char
as a character.
|
--
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! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Wed Feb 25, 2004 2:53 pm Post subject: Re: reinterpret_cast<> |
|
|
In message <4069be0f43c5d3619e70e2c68cc331c1.26421 (AT) mygate (DOT) mailgate.org>,
Aman <aman (AT) techie (DOT) com> writes
| Quote: | 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
cout << "cpt : " << hex << cpt <
|
When you use 'operator <<' applied to an ostream and a char * the result
is an attempt to output a null terminated C-style string. There is no
reason to expect the bytes constituting the binary code for x to be such
a string. Indeed there are excellent reasons to suppose that it isn't.
You might also note that none of the bytes encoding x represent
printable characters in either of the common character encodings (ASCII,
EBCDIC).
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Feb 25, 2004 3:03 pm Post subject: Re: reinterpret_cast<> |
|
|
Aman wrote:
| Quote: | 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 ?
|
There is nothing wrong with the initialisation of cpt.
| Quote: | int main()
{
int x=0x01020304 ;
int* ipt = &x ;
cout << "ipt : "<< hex << ipt << endl ; // ok ---
char* cpt = reinterpret_cast
cout << "cpt : " << hex << cpt <
|
If you write a char-pointer to an ostream, it is treated as a pointer
to a null-terminated string. But cpt doesn't point to a
null-terminated string; it points to the bytes {1, 2, 3, 4} or {4, 3,
2, 1} (depending on which architecture you are running SunOS on).
which don't correspond to printable characters. Note that since these
don't include a null byte the formatting function continues to search
for a null byte in the memory after x. This has undefined behaviour,
so don't tell it to do this!
So you need to convert cpt to void * before printing it:
cout << "cpt : " << hex << static_cast
| Quote: | | short int* spt = reinterpret_cast
cout << "spt : " << hex << spt <
|
I'm not convinced that this is generally safe, because the result of
the reinterpret_cast is unspecified and so might not be convertible to
void *.
I wonder whether this program will do what you intend, even with the
output corrected. There is no guarantee that you can use *(short *)&x
to access the least significant 16 bits (or however many there are in
a short) of x, so you cannot use the value of (short *)&x to determine
where the least significant 16 bits are stored. The result of the
cast is likely to be the same address as &x, regardless of whether
the machine is big- or little-endian.
The safe way to determine byte order is to look at documentation.
Failing that, print out the bytes cpt[0] to cpt[sizeof(x)-1], but
first convert them to unsigned int so that they are treated as numbers
and not character codes.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|