C++ Logo

sg12

Advanced search

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

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

| El 26/07/2013 0:26, Gabriel Dos Reis escribió:
| > Ion Gaztañaga <igaztanaga_at_[hidden]> 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 :-)
|
| But it's not callable by the user to copy arbitrary bytes of memory.

Arbitrary bytes of memory don't have semantics. If you don't need
semantics, you probably want to write your own storage copier, hence
any semantics baggage.

| > | 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 :-)
|
| Sadly, I'm not an important customer, and It wouldn't be portable.

"Important" does not necessarily mean in terms of money or power. It
could be in terms of userbase that your compiler supplier wants to
conquer.

| ¿How
| can a C/C++ programmer inspect a network packet in a freestanding
| implementation (e.g.when programming a DSP)? Should the programmer
| accept a performance penalty and copy byte per byte?

The standard does not and cannot mandate absence of "performance penalty",
even if it mandated std::memcpy as part of freestanding implementation;
it can't mandate it to be faster that "copy byte per byte" --
clock-speed isn't an observable behavior regulated by the standards.

| Should the programmer write assembler code?

Programmers use tools they deem appropriate for the task.

| It's a bad idea to make std::memcpy a requirement for freestanding
| implementations (assuming std::memcpy call is inlined, otherwise the
| performance penalty would be noticeable)?

Would it be useful? I think so. Is it a bad idea? I don't know.

| It's a bad idea that the programmer could have access to the memcpy
| implementation that the compiler is already using for trivial types?

Isn't that std::memcpy? :-)

-- Gaby

Received on 2013-07-26 16:55:09