• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
noatom

reinterpret_cast problem

15 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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

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

0

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.

0

Share this post


Link to post
Share on other sites

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

0

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

0

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites


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

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  
Followers 0