Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 'use reinterpret_cast to group neighbored values together' defined inc++ standard?

Reply
Thread Tools

'use reinterpret_cast to group neighbored values together' defined inc++ standard?

 
 
Marcel Müller
Guest
Posts: n/a
 
      01-07-2013
On 07.01.2013 10:16, Bart van Ingen Schenau wrote:
> The results of this program are not defined by the C++ language. By using
> reinterpret_cast and dereferencing the result, you have placed yourself
> completely at the mercy of the implementation.
>
> Although they are unlikely to take advantage of it, an implementation is
> allowed to put padding between the A and B members of segment, which
> would cause your code to fail.


In fact he expected that segment and vec share a binary compatible
memory layout. This is not guaranteed.

But what about

struct segment {
vec v[2];
};

or optionally

struct segment {
vec v[2];
vec& A() { return v[0]; }
vec& B() { return v[1]; }
};

I think this should be safe since the cast now goes to an array slice.


Marcel
 
Reply With Quote
 
 
 
 
Marek Borowski
Guest
Posts: n/a
 
      01-07-2013
On 2013-01-06 23:39, Ian Collins wrote:
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> Please wrap your lines!
>
>> First thank you for your answer, but you misunderstood me.
>>
>> I do know that my example will compile with any compiler.
>>
>> I wanted to know, if this mini programm does work exactly the same
>> (with the same outputs), when I compile it with different compilers.

>
> As posted, it won't compile with any compiler.
>
> As I said in my reply (that you forgot to quote), all reinterpret_cast
> does is tell the compiler to interpret a bunch of bits as a particular
> type.

We can assume that everybody knows that.

> If the data pointed to happens to be of a compatible type and
> alignment, it will probably work. If not, it won't.
>

Question should be if there is guarantee that data placement is done in
the same way among different compilers. If not reinterpret_cast is not
portable at all.

Regards

Marek

 
Reply With Quote
 
 
 
 
Satish Kumar
Guest
Posts: n/a
 
      5 Days Ago
Searching Candidate who is having 5+ Exp on C++ for south India Location
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
reinterpret_cast<> Aman C++ 15 02-25-2004 03:03 PM
reinterpret_cast<>() v. static_cast<>() Scott Brady Drummonds C++ 11 01-20-2004 09:12 PM
reinterpret_cast - to interpret double as long Suzanne Vogel C++ 17 07-07-2003 02:50 PM



Advertisments