C++ Logo

sg12

Advanced search

Re: [ub] Type punning to avoid copying

From: Gabriel Dos Reis <gdr_at_[hidden]>
Date: Fri, 26 Jul 2013 10:30:58 -0500
Ion Gaztañaga <igaztanaga_at_[hidden]> writes:

[...]

| ¿How can we tell the compiler that a memory buffer is really a different
| type?

Invoke a constructor to turn the raw memory into an object of the
desired type.

| This is a typical situation when handling network packets, mapping
| files or shared memory, when passing data though message queues.
| Situations where we expect C/C++ will be extremely efficient.

C++ revolves about objects, lifetime, types, etc. An object need to be
constructed anyhow.

| We can tell the compiler to avoid some optimizations via "volatile" or
| "atomic<>". Maybe another mechanism is needed to tell the compiler
| (provided the memory region is correctly aligned for the type) that it
| should reinterpret the memory region as the a new type (without calling
| placement new on the buffer, which could overwrite the bits written in
| the buffer).

What is wrong with the existing abstraction mechanisms for turning raw
memory into objects?

-- Gaby

Received on 2013-07-26 17:31:14