questions on basic endianism program

J

Jim Langston

Heinz Ozwirk said:
The one in "testNum = 1" is not a digit. It is an integral number. '1'
would be a digit and that would be represented as the number 49, at least
on a Windows box. What your program prints is the character with the value
1, and that might look like a face sticking on some machines. To print a
number, you have to tell your machine to do so:

cout << static_cast<int>(*ptr);

That would kinda defeat the purpose of the excersize though. Taking the
address of an int and displaying it as an int isn't going to tell him
anything about endianism. What you can do, however, is display each byte
value.

int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

for ( int i = 0; i < sizeof int; ++i )
std::cout << static_cast<int>(ptr) << " ";
std::cout << std::endl;

This would display the value of the first byte, then the second, the third
and the forth.
 
J

Jim Langston

Heinz Ozwirk said:
That would be correct if ptr were a pointer to an int. But it is a pointer
to a char (or the OP wouldn't have ssen those silly faces on his screen).
The expression you are mis-interpreting first fetches the content of some
char variable (*ptr) and then converts it to an int (static_cast) and that
does provide some information about the machine's endiness. Your objection
would be valid only if I'd have written *static_cast<int*>(ptr) instead,
but that is something completly different.

Hmm.. same as
std::cout << static_cast<int>(ptr[0]);

Yes, I missed that.
 
P

pauldepstein

#include <iostream>
using namespace std;

int main()
{
int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

cout << (*ptr);
cin.get();
}

Above is a program to test the endianism of a processor. Since most
PC's are little endian, the answer displayed is usually 1 on a P.C. In
my case (Windows XP, Bloodshed Dev C++) I don't get a 1 but a strange
character which looks a bit like a zero with a smiley inside it.
However, I know that this is essentially right because when I replace
(*ptr) by (*ptr) + 1, I get the expected value of 2. It is only the 1
digit that looks strange. Does anyone know why I get this strange
character? (Or maybe this is OT because it's not a c++ phenomenon.)

The second question is more c++-related.

I replaced ptr = (char*)(&testNum); by

ptr = static_cast<char*>(&testNum); and got an invalid type conversion.
Why? What is the problem with my line? I thought I was just
translating from C to C++.

Apparently static_cast can't cast from type int* to type char*. What's
the general rule about when you can static_cast and when you can't?

Thanks for your help.

Paul Epstein
 
H

Heinz Ozwirk

#include <iostream>
using namespace std;

int main()
{
int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

cout << (*ptr);
cin.get();
}

Above is a program to test the endianism of a processor. Since most
PC's are little endian, the answer displayed is usually 1 on a P.C. In
my case (Windows XP, Bloodshed Dev C++) I don't get a 1 but a strange
character which looks a bit like a zero with a smiley inside it.
However, I know that this is essentially right because when I replace
(*ptr) by (*ptr) + 1, I get the expected value of 2. It is only the 1
digit that looks strange. Does anyone know why I get this strange
character? (Or maybe this is OT because it's not a c++ phenomenon.)

The one in "testNum = 1" is not a digit. It is an integral number. '1' would
be a digit and that would be represented as the number 49, at least on a
Windows box. What your program prints is the character with the value 1, and
that might look like a face sticking on some machines. To print a number,
you have to tell your machine to do so:

cout said:
The second question is more c++-related.

I replaced ptr = (char*)(&testNum); by

ptr = static_cast<char*>(&testNum); and got an invalid type conversion.
Why? What is the problem with my line? I thought I was just
translating from C to C++.

A static_cast can convert between related types like ints and chars or
pointers to base classes and pointers to derived classes, but not between
unrelated types like pointers to int and pointers to char. Basically you can
use a static_cast to convert from a type T1 to another type T2 if there is
an implicit or user defined conversion to convert from T2 to T1, or when you
explicitly ask for an implicit conversion in situations where the language
does not require one. As in the example above, the compiler knows how to
print a char, so there's no need to use an implicit conversion, but if you
want the it to print the character's value, you have to say so, explicitly
casting the char to an int.
Apparently static_cast can't cast from type int* to type char*. What's
the general rule about when you can static_cast and when you can't?

If you have to cast, try a static_cast first. If the compiler complains,
think about what you want to do and once you are sure you know what you are
doing, use reinterpret_cast.

HTH
Heinz
 
M

Moonlit

Hi,

--



#include <iostream>
using namespace std;

int main()
{
int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

cout << (*ptr);
cin.get();
}

Above is a program to test the endianism of a processor. Since most
PC's are little endian, the answer displayed is usually 1 on a P.C. In
my case (Windows XP, Bloodshed Dev C++) I don't get a 1 but a strange
character which looks a bit like a zero with a smiley inside it.
However, I know that this is essentially right because when I replace
(*ptr) by (*ptr) + 1, I get the expected value of 2. It is only the 1

Because it is a pointer to character it will display whatever character 1 is
not the character for one which is I believe somehigng like 49
(if this doesn't make send make it testNum = 49 or lookup the ascii code
for '1')
digit that looks strange. Does anyone know why I get this strange
character? (Or maybe this is OT because it's not a c++ phenomenon.)

The second question is more c++-related.

I replaced ptr = (char*)(&testNum); by

ptr = static_cast<char*>(&testNum); and got an invalid type conversion.
Why? What is the problem with my line? I thought I was just
translating from C to C++.

I think that should be reinterpret_cast<char*>( )

since you are casting a pointer. With static_cast the compiler converts one
type in the other and has to know how to do that.
Apparently static_cast can't cast from type int* to type char*. What's
the general rule about when you can static_cast and when you can't?

Thanks for your help.

Paul Epstein

Regards, Ron AF Greve

http://moonlit.xs4all.nl
 
H

Heinz Ozwirk

Jim Langston said:
That would kinda defeat the purpose of the excersize though. Taking the
address of an int and displaying it as an int isn't going to tell him
anything about endianism.

That would be correct if ptr were a pointer to an int. But it is a pointer
to a char (or the OP wouldn't have ssen those silly faces on his screen).
The expression you are mis-interpreting first fetches the content of some
char variable (*ptr) and then converts it to an int (static_cast) and that
does provide some information about the machine's endiness. Your objection
would be valid only if I'd have written *static_cast<int*>(ptr) instead, but
that is something completly different.

Heinz
 
F

Fat Ass Fred

#include <iostream>
using namespace std;

int main()
{
int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

cout << (*ptr);
cin.get();
}

Change char *ptr; to int *ptr;
Change ptr = (char *)(&testNum); to ptr = (int *)(&testNum);

Net Result shows what you expected to happen as you have it typed, you
see a smily face, or the character whose ascii code is 1. testNum is
an INTEGER (2-bytes) and char * is a CHARACTER (1-byte), your lucky to
be using a Little Endian machine, had you been using a BigEndian, it'd
been a long time of incrementing before you where able to see
anything.....
 
P

pauldepstein

Heinz Ozwirk wrote:
....
A static_cast can convert between related types like ints and chars or
pointers to base classes and pointers to derived classes, but not between
unrelated types like pointers to int and pointers to char. Basically you can
use a static_cast to convert from a type T1 to another type T2 if there is
an implicit or user defined conversion to convert from T2 to T1, or when you
explicitly ask for an implicit conversion in situations where the language
does not require one. As in the example above, the compiler knows how to
print a char, so there's no need to use an implicit conversion, but if you
want the it to print the character's value, you have to say so, explicitly
casting the char to an int.

....

But, in that case, why is it possible to cast from int* to char* using
(char*) &testNum

This shows that there are differences in functionality between the
older notation with (char*) and the newer syntax with static_cast.

However, my (possibly poor) texts gave me the impression that the
difference was only one of syntax.

Can anyone explain to me the difference between casts of the form

x = (new_type) expression; and

x = static_cast<new_type> (expression);

Thank you,

Paul Epstein
 
H

Heinz Ozwirk

Heinz Ozwirk wrote:
...

...

But, in that case, why is it possible to cast from int* to char* using
(char*) &testNum

One of the goals of C++ has been to allow most C programs to be compiled
with a C++ compiler, so we must live with all the little problems of C.
This shows that there are differences in functionality between the
older notation with (char*) and the newer syntax with static_cast.

Whoever said there is no difference, didn't know what he is talking about.
However, my (possibly poor) texts gave me the impression that the
difference was only one of syntax.

So they are indeed poor texts. Get some decent book, perhaps "The C++
Programming Language" by Bjarne Stroustup (3rd edition, IIRC)
Can anyone explain to me the difference between casts of the form

x = (new_type) expression; and

x = static_cast<new_type> (expression);

That should fill an entire chapter in a good book. But for a start, this
might help: http://msdn2.microsoft.com/en-us/library/c36yw7x9.aspx or just
search the web for "static_cast".

HTH
Heinz
 
B

Bo Persson

Heinz Ozwirk wrote:
...

...

But, in that case, why is it possible to cast from int* to char*
using
(char*) &testNum

It might not be!

Using the (char*) cast, you tell the compiler that the cast is ok.
"Trust me, I know what I'm doing!"
This shows that there are differences in functionality between the
older notation with (char*) and the newer syntax with static_cast.

However, my (possibly poor) texts gave me the impression that the
difference was only one of syntax.

Can anyone explain to me the difference between casts of the form

x = (new_type) expression; and

This is "Shut up and do it!".
x = static_cast<new_type> (expression);

Here the compiler will tell you if doesn't work!


Bo Persson
 
P

pauldepstein

Heinz said:
One of the goals of C++ has been to allow most C programs to be compiled
with a C++ compiler, so we must live with all the little problems of C.


Whoever said there is no difference, didn't know what he is talking about.


So they are indeed poor texts. Get some decent book, perhaps "The C++
Programming Language" by Bjarne Stroustup (3rd edition, IIRC)


That should fill an entire chapter in a good book. But for a start, this
might help: http://msdn2.microsoft.com/en-us/library/c36yw7x9.aspx or just
search the web for "static_cast".

HTH
Heinz

Thanks, Heinz.

If I can focus on the literature aspect. Actually, the fault lies not
in the text but in the reader, that he misremembered. The text says no
such thing.

The text I am using is C++ Primer, 4th Ed. Is this a good book?

I do have a few concerns about it. For example, here is a suggested
coding fragment for a handler:

while (cin >> item1 >> item2) {
try { // code to add two Sales_Items together and generate an error if
this fails
} catch (runtime_error err){
// remind user that ISBN must match and prompt for another pair
cout << err.what()
<< "\n Try Again? Enter y or n" << endl;
char c;
cin >> c;
if (cin && c == 'n')
break;
}
}

So what is my problem? Is this not stellar coding? Well, my concern
is... How about that catchy little tune that is so prevalent on TV ad
jingles -- "Throw by value, catch by reference" ??

(Rather than just say "they catch by value and not by reference", I
thought I would display the catch in full context.)

Is this code ok, or is catching by value poor practice here?

Paul Epstein
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,113
Latest member
Vinay KumarNevatia
Top