473,769 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char *, unsigned char * and POD types

Hi, at first the code doesn't seem to work. Any ideas?:
#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?
Thanks in advance.
Dec 13 '07 #1
11 2953
john wrote:
Hi, at first the code doesn't seem to work.
Define "doesn't seem to work".
Any ideas?:
#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';
This will print the bytes as characters.
>
}

Two more questions.

Q1) Is the above char * use guaranteed to work with all POD types?
IIRC, the result of the reinterpret_cas t is unspecified.
Q2) If I remember well, unsigned char * covers more types. Am I wrong,
and it covers only POD types?
It's the same for char and unsigned char. Only POD types are covered.

Dec 13 '07 #2
Rolf Magnus wrote:
john wrote:
>Hi, at first the code doesn't seem to work.

Define "doesn't seem to work".
>Any ideas?:
#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';

This will print the bytes as characters.
Right, what did you (the OP) expect it to output? On most computers, one
character would be 0x07 and the other 3 0x00 or nulls. If you wanted to see
the value of the bytes (the numerical value) you'll need to cast the char to
a number.
cout<< static_cast<int >( p[i] ) << '\n';
may give you what you expect, although you haven't stated what you expect so
I can only guess. You may want to use static_cast<uns igned int>.
>}

Two more questions.

Q1) Is the above char * use guaranteed to work with all POD types?

IIRC, the result of the reinterpret_cas t is unspecified.
AFAIK there is no set requirement for a C or C++ program to store their
numbers in any particular way as long as they follow the requirements, I.E.
sizeof char <= sizeof int <= sizeof long int etc... A computer could use
whatever internal storage it deems best. So the output of the program is
unspecified, and for a fact on bigendian and little endian machines you will
get different outputs.
>Q2) If I remember well, unsigned char * covers more types. Am I
wrong, and it covers only POD types?

It's the same for char and unsigned char. Only POD types are covered.
I don't understand the question. "covers more types" of what? Basically
what you are trying to do (or so I think) is close, you just need to cast
the character to a number to see the value of it.

--
Jim Langston
ta*******@rocke tmail.com
Dec 13 '07 #3
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.

If you're trying to print the bytes of an object, then the following
code is perfectly well-defined and portable:

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

--
Tomás Ó hÉilidhe
Dec 13 '07 #4
"Tom" 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.
AFAIK, only unsigned char * and char * are guaranteed to work for
getting the byte values of a POD, and not a signed char *.

If you're trying to print the bytes of an object, then the following
code is perfectly well-defined and portable:

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

I think volatile is not necessary for non volatile PODs, and I think
here is an overkill, or am I missing something?
Dec 13 '07 #5
john wrote:
>
AFAIK, only unsigned char * and char * are guaranteed to work for
getting the byte values of a POD, and not a signed char *.

> If you're trying to print the bytes of an object, then the
following code is perfectly well-defined and portable:

#include <iostream>

template<cla ss 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_ca st is perfectly well-defined.


I think volatile is not necessary for non volatile PODs, and I think
here is an overkill
==for this reason,
or am I missing something?
Dec 13 '07 #6
john <jo**@no.spamwr ote in comp.lang.c++:
AFAIK, only unsigned char * and char * are guaranteed to work for
getting the byte values of a POD, and not a signed char *.

You might be right... I hadn't thought about it coz I'd never use a signed
char for that purpose. I know signed char can't have padding bits, but I
wonder if it can still have invalid bit patterns (negative zero and all
that lark).

If dealing with two's complement, I don't see any reason why you couldn't
use signed char

I think volatile is not necessary for non volatile PODs, and I think
here is an overkill, or am I missing something?

I just stuck it in so you could use it on anything.

--
Tomás Ó hÉilidhe
Dec 13 '07 #7
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
Dec 14 '07 #8
James Kanze <ja*********@gm ail.comwrote in comp.lang.c++:
> 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:-).

We're printing unsigned char's, not char's. That should result in numbers
being printed rather than characters... right?

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

That's just for people who wet the bed at the thought of reinterpret
cast.

Every object is made up of bytes -- *every* object. The reinterpret cast
here is the perfect candidate for the job.

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.

Again, I would have expected unsigned char's to result in the printing of
numbers instead of characters.

--
Tomás Ó hÉilidhe
Dec 14 '07 #9
On Dec 14, 4:57 pm, "Tomás Ó hÉilidhe" <t...@lavabit.c omwrote:
James Kanze <james.ka...@gm ail.comwrote in comp.lang.c++:
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:-).
We're printing unsigned char's, not char's. That should result
in numbers being printed rather than characters... right?
No. (You've got a point that it probably should, but for
various historical reasons...)
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 ) ).
That's just for people who wet the bed at the thought of
reinterpret cast.
Every object is made up of bytes -- *every* object. The
reinterpret cast here is the perfect candidate for the job.
Except that the standard doesn't say so. Practically speaking,
the standard doesn't even guarantee that you can
reinterpret_cas t a char* to an int* without getting a core dump.

Realistically, the standard doesn't say so, because it wants
reinterpret_cas t to be pragmatically useful, and what it takes
to be pragmatically useful depends very much on the machine
architecture. IMHO, the intent is clear, and I don't worry
about using it when I'm working this close to the hardware.

--
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
Dec 14 '07 #10

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

Similar topics

2
3421
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has raised a doubt in my mind on the same issue. Either through ignorance or incompetence, I've been unable to resolve some issues. 6.4.4.4p6 states...
9
4177
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have signed char which is -256. Does it means that we can assign the same ASCII value to both signed and unsigned. That means the ASCII value can be represented with a type of signed char and also unsigned char? For example int main(void) {
10
15662
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
22
5621
by: juanitofoo | last post by:
Hello, I've just switched to gcc 4 and I came across a bunch of warnings that I can't fix. Example: #include <stdio.h> int main() { signed char *p = "Hola";
4
1042
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok etc and their implementation.the way compiler treats them and the scenarios where one
6
45821
by: Steven Jones | last post by:
Can anybody illustrate the usefulness of having char and unsigned char? I mean, under what circumstances would one want to use unsigned char (or unsigned char *) rather than char (or char *, respectively)?
3
14581
by: uche | last post by:
Please give me some feed back on this issue: Here is the complier error: hexdmp.cpp: In function `void output(unsigned char, int, bool&)': hexdmp.cpp:133: error: invalid types `unsigned char' for array subscript hexdmp.cpp:146: error: invalid types `unsigned char' for array subscript hexdmp.cpp:146: error: invalid types `unsigned char' for array
12
3586
by: Martin Wells | last post by:
I'm trying to come up with a fully-portable macro for supplying memset with an unsigned char rather than an int. I'm going to think out loud as I go along. . . I'll take a sample system before I begin: CHAR_BIT == 16 sizeof(short) == sizeof(int) == 1 Assume none of the integer types have padding bits Sign-magnitude
43
17230
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.