Basic question about casting and addresses

D

drowned

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 << endl;
cout << "ucp[" << i << "] = "; printBinary(ucp); 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?
 
G

Gianni Mariani

drowned said:
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 << endl;
cout << "ucp[" << i << "] = "; printBinary(ucp); 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
 
K

Kevin Goodsell

drowned said:
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 << endl;
cout << "ucp[" << i << "] = "; printBinary(ucp); 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
 
R

Roger Willcocks

....
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 << endl;
cout << "ucp[" << i << "] = "; printBinary(ucp); cout << endl;
}
}

You probably mean

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

Rolf Magnus

drowned said:
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 said:
for(int i=0;i<sizeof(float);i++) { //sizeof(float) == 4
cout << "ucp[" << i << "] = " << (long)ucp << endl;
cout << "ucp[" << i << "] = "; printBinary(ucp); 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.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top