[ub] Type punning to avoid copying

Gabriel Dos Reis gdr at cs.tamu.edu
Fri Jul 26 17:30:58 CEST 2013


Ion Gaztañaga <igaztanaga at gmail.com> 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



More information about the ub mailing list