[ub] [c++std-core-23865] Re: unions and undefined behavior

Gabriel Dos Reis gdr at axiomatics.org
Fri Jul 26 00:26:14 CEST 2013


Ion Gaztañaga <igaztanaga at gmail.com> writes:

[...]

| > Note also that with clang and gcc, you can call __builtin_memcpy to
| > get defined and optimized behavior even without #including <string.h>.
| > It may be replaced with a library call if the compiler doesn't
| > optimize it, and your certified compiler may not provide the same
| > feature.
| 
| Thanks for the explanation. A built-in memcpy is one of the language 
| features that I miss,

The language has a builtin memcpy: it is variously called assignment
(operator=), or copy-construction depending on the type :-)

|  as it seems to be the only way to do bit-casts 
| that are essential in some embedded domains (inspecting network packets, 
| data serialization...). A conforming freestanding implementation could 
| also include memcpy.

Indeed.  I'm pretty sure if you are an important customer for your
compiler supplier, you can convince him or her to include it :-)

| That would not fix the problem for safety-critical Sw

Why? 

| but would be a good low-level tool for embedded developers that don't 
| want to write their own memcpy function in assembler.

Most C++ compilers treat std::memcpy as something fundamental.
Other considerations may force a given compiler's backend to emit a
function call, but the semantics is wired into the core language. 
Some compilers even implement assignment (at the C++ source level) in
terms of std::memcpy().

-- Gaby



More information about the ub mailing list