Sign in to follow this  
noatom

reinterpret_cast problem

Recommended Posts

noatom    927

I'm reading a book,and I have an exercise that gives me problems.Here's the code:

 

double aha [5] = {0,0,0,0,0};
unsigned char* ma = reinterpret_cast<unsigned char*>(&aha[0]);
	memset(ma,1,sizeof(double));
function_to_cout_all_elements_of_aha(aha);

 

 

The problem is,it prints this:

7.784e-304

0

0

0

0

 

What's happening to the first element?! Why the other 4 are fine,and the first one got screwed up?

Share this post


Link to post
Share on other sites
Brother Bob    10344

The pointer ma points to the beginning of the aha array. The memset function then writes "something" to the first double of ma, which is the first double of the aha array. That is why the first element of aha is borked, and the remaining four elements are not

Share this post


Link to post
Share on other sites
jnmacd    197
Well you are pointing to the first element in the array, then modifying only sizeof(double) bytes.
Any memory after sizeof(double) bytes should be unaffected.
 
To modify all,
 
memset(ma,1,sizeof(double)*5);
This has nothing to do with reinterpret_cast.

Share this post


Link to post
Share on other sites
TheChubu    9446

Well you are pointing to the first element in the array, then modifying only sizeof(double) bytes.
Any memory after sizeof(double) bytes should be unaffected.
 
To modify all,
 

memset(ma,1,sizeof(double)*5);
This has nothing to do with reinterpret_cast.

Wouldn't that just bork all of the array instead of just the first double?

Share this post


Link to post
Share on other sites
fastcall22    10838

Still,the exercise asks: Why was not each element set to 1? I have no ideea how to explain it

Because you aren't setting each double to 1.0 -- you are setting each individual byte for the first double to 0x01. The layout for your double will be 0x0101010101010101, which is around 7.784e-304, according to IEEE-754.

You probably want to use std::fill instead:
double arr[4] = {};std::fill( arr, arr+4, 1. );

(Also, 1.0 looks like 0x3FF0000000000000 in memory.)

Share this post


Link to post
Share on other sites

It says this:

after using reinterpret cast .... set each byte of the array to 1

 

The memset you used does this: sets each byte of the first sizeof(double) chars in the array to 1. You need to use sizeof(double) * 5 to set all the array bytes to 1.

Share this post


Link to post
Share on other sites
noatom    927

it's supposed not to put 1 in all,because the question is why isn't there 1 in each element.

 

I also used the sizeof(double)*5,and no,you don't get 1,you get the same thing i showd you above.Nypyren might be right,that's the goal,still pretty hard to get anyway.

 

but fastcall gave an alternative...

Share this post


Link to post
Share on other sites

You do get all the array BYTES (well, chars, but they are usually the same thing) set to 1. That doesn't mean the double is set to 1 though.

 

If you tried it with an array of 5 unsigned short instead of doubles you would get 0x0101 = 257 in each short, not 1 either. (Again, assuming 16 bit unsigned short).

Share this post


Link to post
Share on other sites
Matias Goldberg    9573

I'm reading a book,and I have an exercise that gives me problems.Here's the code:
 

double aha [5] = {0,0,0,0,0};
unsigned char* ma = reinterpret_cast<unsigned char*>(&aha[0]);
	memset(ma,1,sizeof(double));
function_to_cout_all_elements_of_aha(aha);
 
The problem is,it prints this:
7.784e-304
0
0
0
0
 
What's happening to the first element?! Why the other 4 are fine,and the first one got screwed up?

If you want to write to all 5 elements, you would have to change your memset function:

memset(ma,1,sizeof(double) * 5);

Note the "*5" multiplication (that's because there are 5 doubles)
Size of double is usually 8 bytes, so 8 bytes * 5 elements = memset 40 bytes

As for the question in the book, it's trying to make you realize what's going on.
The first double in memory is filled like this: "0x0101010101010101", but it doesn't mean that the double will contain "01", not even "101010101010101" or something like that.

If you play with an online binary to floating point conversor and see how the value "0x01010101" converts to "2.3694278E-38". Note that the conversor works with 32-bit floats, not with 64-bit doubles.

 

The only exception is when doing memset( ma, 0, sizeof(double) * 5 ); because that sets the double(s) to 0x0000000000000000; and IEEE 754-compatible floats & doubles have the property that this value corresponds to "0.0", which makes memset a great way to zero your array. But you can't use it to set it to any other values, because then it won't match.

 

Hope this explanation helps

Share this post


Link to post
Share on other sites
jnmacd    197


Well you are pointing to the first element in the array, then modifying only sizeof(double) bytes.
Any memory after sizeof(double) bytes should be unaffected.
 
To modify all,

memset(ma,1,sizeof(double)*5);
This has nothing to do with reinterpret_cast.


Wouldn't that just bork all of the array instead of just the first double?


Yes, he asked why only the first got screwed up. This will screw all of them up.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this