Tomás Ó hÉilidhe wrote:
john <jo**@no.spamwr ote in comp.lang.c++:
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
int x= 7;
char *p= reinterpret_cas t<char *>(&x);
for(size_t i= 0; i< sizeof(x); ++i)
cout<< p[i]<< '\n';
}
Two more questions.
Q1) Is the above char * use guaranteed to work with all POD types?
Q2) If I remember well, unsigned char * covers more types. Am I wrong,
and it covers only POD types?
A "plain" char should only be used for storing characters. If you
want to use a byte for a different purpose (e.g. storing numbers), then
go with unsigned char or signed char.
For clarity's sake. As far as the standard is concerned, char,
unsigned char and signed char are all (small) integral types.
Using plain char only for characters, the other two when you
want small integers, and unsigned char for raw memory, is a good
convention however.
If you're trying to print the bytes of an object, then the following
code is perfectly well-defined and portable:
Not unless the bytes in the object all correspond to printable
characters:-).
#include <iostream>
template<class T>
void PrintBytes(T const &obj)
{
char unsigned const volatile *p =
reinterpret_cas t<char unsigned const volatile*>(&obj );
char unsigned const volatile *const pend = p + sizeof obj;
do std::cout << *p++;
while (pend != p);
}
Despite someone has suggested to the contrary, the behaviour of the
reinterpret_cas t is perfectly well-defined.
Sort of. Formally, I don't think that the standard guarantees
the above; you'd have to replace the reinterpret_cas t with
static_cast< unsigned char const* >( static_cast< void const* >(
&obj ) ). Practically, there are enough other constraints on
reinterpret_cas t that I can't imagine an implementation where it
didn't work (and it's what I also use).
On the other hand, outputting non-printable characters to a
stream opened in text mode is undefined behavior, and what
actually gets output also depends on the system. If obj is an
int with the value 10, for example, the above code outputs 4
bytes under Linux, 5 under Windows, both on an Intel PC.
I'm also dubious about the utility of volatile here.
I use a class template template Dump (and a function template
which returns it, for type induction) with the following
function:
template< typename T >
Dump< T >::Dump(
T const& obj )
: myObj( reinterpret_cas t< unsigned char const* >( &obj ) )
{
}
template< typename T >
void
Dump< T >::print(
std::ostream& dest ) const
{
IOSave saver( dest ) ;
dest.fill( '0' ) ;
dest.setf( std::ios::hex, std::ios::basef ield ) ;
unsigned char const* const
end = myObj + sizeof( T ) ;
for ( unsigned char const* p = myObj ; p != end ; ++ p ) {
if ( p != myObj ) {
dest << ' ' ;
}
dest << std::setw( 2 ) << (unsigned int)( *p ) ;
}
}
(The class provides an operator<< which calls this function, and
there is an function template which returns an instance of the
class, to exploit type deduction, so you can write things like:
int i = 10 ;
std::cout << "value = " << i
<< " (" << Gabi::dump( i ) << ")n" ;
and see something like:
value = 10 (00 00 00 0a)
.)
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34