This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Optimization issue
- From: Segher Boessenkool <segher at kernel dot crashing dot org>
- To: Juan Cabrera <jjcp dot 91 at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 27 Nov 2017 19:39:30 -0600
- Subject: Re: Optimization issue
- Authentication-results: sourceware.org; auth=none
- References: <CAJRqx1zpTGaCggtgyVzxdSACCfzj43FUKVViWLys2TeBePpZEQ@mail.gmail.com>
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