• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

reinterpret_cast problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

15 replies to this topic

#1noatom  Members

Posted 13 March 2013 - 11:39 AM

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?

#2Brother Bob  Moderators

Posted 13 March 2013 - 12:28 PM

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

#3jnmacd  Members

Posted 13 March 2013 - 12:29 PM

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.

#4noatom  Members

Posted 13 March 2013 - 01:27 PM

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

#5ApochPiQ  Moderators

Posted 13 March 2013 - 01:37 PM

Do you know what the memset() call does?
Wielder of the Sacred Wands

#6noatom  Members

Posted 13 March 2013 - 01:41 PM

changes the value of the bytes

#7Nypyren  Members

Posted 13 March 2013 - 01:43 PM

http://en.wikipedia.org/wiki/Binary64

Read the "Double Precision Examples" section. Now, what you're doing is you're filling the bytes with: 01 01 01 01 01 01 01 01

The bytes are not being CASTED back to doubles. You are directly modifying the 8 bytes INSIDE each double.

Edited by Nypyren, 13 March 2013 - 01:51 PM.

#8TheChubu  Members

Posted 13 March 2013 - 01:46 PM

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?

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

#9fastcall22  Moderators

Posted 13 March 2013 - 01:47 PM

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.)
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#10noatom  Members

Posted 13 March 2013 - 01:50 PM

It says this:

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

Posted 13 March 2013 - 02:02 PM

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

#12Nypyren  Members

Posted 13 March 2013 - 02:04 PM

Perhaps your book is *intentionally* trying to show you what happens if you manipulate bytes inside of larger data types?

#13noatom  Members

Posted 13 March 2013 - 02:06 PM

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

Posted 13 March 2013 - 02:10 PM

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

#15Matias Goldberg  Members

Posted 13 March 2013 - 05:59 PM

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

#16jnmacd  Members

Posted 15 March 2013 - 11:35 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.