开发者

Boolean_conversions Using reinterpret_cast<>

Can any one Give the example for

 Boolean_conversions Using  reinterpret_cast<>

Iam getting the execution failure..Any one explain ..

Ex:

   void Boolean_Conversions()
     {
         bool b = 0;
         int *i = reinterpret_cast<int*> (&b);
  // iam expecting as *i is 0/false    ,but it is showing runtime error          
     }

Edit: In ISO Standard Section $5.2.10, para 1:

      "The result of the expression reinterpret_cast<T>(v) is the
  result of converting the expression v to type T. If T is a
  reference type, the result is an lvalue; otherwise, the re
  sult is an rvalue and the l-value to r-value (4.1), array
  to pointer (开发者_Go百科4.2), and function to pointer (4.3) standard c
  onversions are performed on the the expression v. Types sh
  all not be defined in a reinterpret_cast."

From this Iam trying ..I did many conversions ..like Array_to_Pointer,Function_To_Pointer,Integral/Pointer_Conversions ,etc...But i don't know why it is failing For Boolean Conversions.

Can any one tell why it is failing ..Otherwise Tell the Boolean COnversions using reinterpret_cast.

Mostly It is Giving Run time failure in G++.CC/EDG/COdepad..etc compilers

Please Explain


No, the output is not defined. Note that the conversion is undefined.

On my machine *i = 0xcccccc00

Note the lsb which is 00

which corresponds to the value of the bool variable initialized with 0(false). The byte pattern in the other three bytes (assuming the size of int as 32-bits) is completely unspecified and depends on what is there in that memory/endianness etc.

Technically, however that is an undefined behavior because we are deferencing a memory location (all of) which we did not reserve/allocate.


You've still not made it clear what you are trying to do! Are you aware for example that there are three casts:

TO_TYPE DEST_VAL = static_cast<TO_TYPE>(SOME_VAL);

TO_TYPE DEST_VAL = dynamic_cast<TO_TYPE>(SOME_VAL);

and the one you're hung up on:

TO_TYPE DEST_VAL = reinterpret_cast<TO_TYPE>(SOME_VAL);

Of these three, the last is the most dangerous, basically you are ignoring all the things the compiler is telling you about types and forcing your way on it! It implies that you know what you are doing, and in most cases, you don't.

Typically you should only use the last one as a LAST RESORT when the other two casts can't do what you need and you've carefully looked at the design, and consulted soothsayers etc.! It is NOT type safe, and IT MAKES NO GUARANTEES that the resulting object is what you expect it to be, as in what you are trying to do above.. bool and int may or may not have the same aligned storage and pointing to the address of a bool with a pointer that should point to an int and dereferencing could lead to potentially accessing all sorts of nether regions of memory...


bool b = 0;
int *i = reinterpret_cast<int*> (&b);

this will store the memory address of b to i after it is "converted" to a suitable style for an int pointer. if you want you cast a bool to int just do

int i = reinterpret_cast<int> (b);

if b is false, yes indeed it will be 0, otherwise it will be 1. You dont need reinterpret_cast<int> though. Read the other comments made on the thread and it will become clear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜