This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Optimization issue


On Mon, Nov 27, 2017 at 10:02:53PM -0300, Juan Cabrera wrote:
> int read(const void* buffer)
> {
>     int r;
>     std::memcpy(&r, buffer, 1);
>     // std::memcpy(&r, buffer, sizeof(r));
>     return r;
> }

This is undefined behaviour (you are returning uninitialised data).
Try with  int r = 0;  instead?  (It is still not portable then, but
at least not undefined behaviour anymore).

> Do you think there's a reason for this or is this just an optimizer bug?

We do not care too much about generating efficient binary code for
incorrect source code ;-)


Segher


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]