reinterpret_cast problem

Started by
14 comments, last by jnmacd 11 years, 1 month ago

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Advertisement
Perhaps your book is *intentionally* trying to show you what happens if you manipulate bytes inside of larger data types?

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

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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


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.

This topic is closed to new replies.

Advertisement