This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Question about GCC 5.2.0 and expression reordering
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: David Barto <barto at cambridgesemantics dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Wed, 14 Jun 2017 17:06:55 +0100
- Subject: Re: Question about GCC 5.2.0 and expression reordering
- Authentication-results: sourceware.org; auth=none
- References: <CC40CBD3-1E2F-4620-9A8A-68F17699C030@cambridgesemantics.com>
On 14 June 2017 at 16:58, David Barto wrote:
> I have the following code. It is compiled at -O3 using g++
> with the -std=gnu++14 option.
>
> The code in question is the following:
>
> if ( !testbit(rec_scan0,63) && f20type == URI ) {
>
> Valgrind claims that the "Conditional jump or move depends on uninitialized value(s)"
>
> Well, only if !testbit(rec_scan0,63) should we check the f20type value,
> right? Apparently GCC is reordering the expression. This makes no
> sense to me, from the old school of C coding.
>
> My question is 2 fold:
> 1 - is this legal (and I think it is) and if so would someone point to the relevant
> part of the C++ standard. (I can’t find it)
It depends on whether the default && is used, or if the types of the
expressions on either side cause an user-defined operator&& to be
used. The built-in && short-circuits, and the LHS should not be
evaluated if the RHS is false. An overloaded operator&& doesn't
short-circuit.
Assuming the types of the LHS and RHS are both bool, it should
short-circuit and shouldn't evaluate the RHS if the LHS is false.