Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Basic question about casting and addresses

Reply
Thread Tools

Basic question about casting and addresses

 
 
drowned
Guest
Posts: n/a
 
      08-02-2003
I'm having a problem understanding why the code in this little program
I've got is behaving the way it does... if anyone can explain it, I'd
be grateful:


#include <iostream>
using namespace std;

//This function takes an unsigned char (one byte on my machine)
//and prints it in binary notation
void printBinary(const unsigned char val) {
for(int i = 7; i >= 0; i--)
if(val & (1 << i))
std::cout << "1";
else
std::cout << "0";
}


int main() {
float f=1;
unsigned char uc = (unsigned char) &f;
unsigned char* ucp = &uc;
for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
cout << "ucp[" << i << "] = " << (long)ucp[i] << endl;
cout << "ucp[" << i << "] = "; printBinary(ucp[i]); cout << endl;
}
}



If I understand what is going on here correctly, I am taking the
address of the float and casting it to an unsigned char. I then assign
that value to the unsigned char pointer. When I change the value of
the float, however, ucp's value changes. I would think that ucp would
not change because it holds an address... what does changing the value
of the float have to do with its address?
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-02-2003
drowned wrote:
> I'm having a problem understanding why the code in this little program
> I've got is behaving the way it does... if anyone can explain it, I'd
> be grateful:
>
>
> #include <iostream>
> using namespace std;
>
> //This function takes an unsigned char (one byte on my machine)
> //and prints it in binary notation
> void printBinary(const unsigned char val) {
> for(int i = 7; i >= 0; i--)
> if(val & (1 << i))
> std::cout << "1";
> else
> std::cout << "0";
> }
>
>
> int main() {
> float f=1;
> unsigned char uc = (unsigned char) &f;
> unsigned char* ucp = &uc;
> for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
> cout << "ucp[" << i << "] = " << (long)ucp[i] << endl;
> cout << "ucp[" << i << "] = "; printBinary(ucp[i]); cout << endl;
> }
> }
>


Let's see how memory *might* be layed out:

stack_frame : <return address 0>
stack_frame : <return address 1>
stack_frame : <return address 2>
stack_frame : <return address 3>
f : <float byte 0>
f : <float byte 1> <<<<<<<<<< 3 + &uc
f : <float byte 2> <<<<<<<<<< 2 + &uc
f : <float byte 3> <<<<<<<<<< 1 + &uc
uc : <char byte 0> <<<<<<<<<< &uc
filler : <random junk>
filler : <random junk>
filler : <random junk>
ucp : <unsigned char* byte 0>
ucp : <unsigned char* byte 1>
ucp : <unsigned char* byte 2>
ucp : <unsigned char* byte 3>

&uc + 3 bytes is within the float.

>
>
> If I understand what is going on here correctly, I am taking the
> address of the float and casting it to an unsigned char. I then assign
> that value to the unsigned char pointer. When I change the value of
> the float, however, ucp's value changes. I would think that ucp would
> not change because it holds an address... what does changing the value
> of the float have to do with its address?


What your doing is undefined because you take the address of an object
of size 1 and then reference 3 more objects after that. Who knows what
the compiler should or would or has done or not done. What I
demonstrated above is what *SOME* compilers do but it's not even correct
because some compilers *may* not decide to layout memory in this way.

In other words - DON'T do it.

G

 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-02-2003
drowned wrote:
> I'm having a problem understanding why the code in this little program
> I've got is behaving the way it does... if anyone can explain it, I'd
> be grateful:
>
>
> #include <iostream>
> using namespace std;
>
> //This function takes an unsigned char (one byte on my machine)


A char is a byte on all machines. This is required by the C++ standard.

> //and prints it in binary notation
> void printBinary(const unsigned char val) {
> for(int i = 7; i >= 0; i--)
> if(val & (1 << i))
> std::cout << "1";
> else
> std::cout << "0";
> }
>
>
> int main() {
> float f=1;
> unsigned char uc = (unsigned char) &f;


This is not a very meaningful thing to do. The result is very unlikely
to be the address of f, or anything useful for that matter. Also, using
C-style casts is not recommended in most cases. Use C++ cast operators
instead.

> unsigned char* ucp = &uc;
> for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
> cout << "ucp[" << i << "] = " << (long)ucp[i] << endl;
> cout << "ucp[" << i << "] = "; printBinary(ucp[i]); cout << endl;
> }
> }
>
>
>
> If I understand what is going on here correctly, I am taking the
> address of the float and casting it to an unsigned char. I then assign
> that value to the unsigned char pointer.


No. You assign the address of your unsigned char (uc) to your unsigned
char pointer (ucp).

> When I change the value of
> the float, however, ucp's value changes.


I don't know of any reason this would happen, and I see no evidence for
it in the code you posted. Post code demonstrating this if you'd like it
explained.

> I would think that ucp would
> not change because it holds an address... what does changing the value
> of the float have to do with its address?


Nothing. There does not appear to be any relationship at all between the
float f and the unsigned char * ucp.

-Kevin

 
Reply With Quote
 
Roger Willcocks
Guest
Posts: n/a
 
      08-03-2003

"drowned" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
....
> int main() {
> float f=1;
> unsigned char uc = (unsigned char) &f;
> unsigned char* ucp = &uc;
> for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
> cout << "ucp[" << i << "] = " << (long)ucp[i] << endl;
> cout << "ucp[" << i << "] = "; printBinary(ucp[i]); cout << endl;
> }
> }

You probably mean

unsigned char* ucp = (unsigned char *)&f;

--
Roger


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-03-2003
drowned wrote:

> I'm having a problem understanding why the code in this little program
> I've got is behaving the way it does... if anyone can explain it, I'd
> be grateful:
>
>
> #include <iostream>
> using namespace std;
>
> //This function takes an unsigned char (one byte on my machine)


An unsigned char _always_ is one byte. Note however that a byte may be
bigger than 8bits.

> //and prints it in binary notation
> void printBinary(const unsigned char val) {
> for(int i = 7; i >= 0; i--)
> if(val & (1 << i))
> std::cout << "1";
> else
> std::cout << "0";
> }
>
>
> int main() {
> float f=1;
> unsigned char uc = (unsigned char) &f;


Here, you convert the address of f into an unsigned char. Of course
parts of that address will probably be lost, since on most machines,
pointers are bigger than unsigned chars.

> unsigned char* ucp = &uc;


Here, you create a pointer that points to the converted address (or what
is left from it), which is one byte big. I'm quite sure that you wanted
instead to convert the address of the float into a pointer to unsigned
char, for which you'd have to replace your above two lines with:

unsigned char* ucp = reinterpret_cast<unsigned char*>(&f);

> for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
> cout << "ucp[" << i << "] = " << (long)ucp[i] << endl;
> cout << "ucp[" << i << "] = "; printBinary(ucp[i]); cout << endl;
> }


In that loop, you try to read 4 bytes through the above pointer, which
only points to one single unsigned char.

> }
>
>
>
> If I understand what is going on here correctly, I am taking the
> address of the float and casting it to an unsigned char.


Yes.

> I then assign that value to the unsigned char pointer.


No, you assign the address of the resulting unsigned char to the
unsigned char pointer.

> When I change the value of
> the float, however, ucp's value changes. I would think that ucp would
> not change because it holds an address...


How do you change it?

> what does changing the value of the float have to do with its address?


Nothing, I'd say.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
How to implement a firewall for Windows platform that blocks based on Mac addresses instead of IP addresses cagdas.gerede@gmail.com C Programming 1 12-07-2006 04:30 AM
Physical Addresses VS. Logical Addresses namespace1 C++ 3 11-29-2006 03:07 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM



Advertisments