public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc not warning on all occurences of operations with undefined results?
@ 2005-11-15 13:48 Daniel Kabs
  2005-11-15 16:03 ` John Love-Jensen
  2005-11-15 20:33 ` Ladislav Mecir
  0 siblings, 2 replies; 4+ messages in thread
From: Daniel Kabs @ 2005-11-15 13:48 UTC (permalink / raw)
  To: gcc-help

Hello there,

gcc 3.3.5 issues warnings on lines like

  a[i] = i++;
  i = i++;
  *p++ = func(*p);

e.g.
"test_increment.c:19: warning: operation on `i' may be undefined"

I take it, this is because of the C standard saying, that if I have an 
expression and somewhere in that expression, I use an increment (decrement) 
operator on a variable, I can not mention that variable elsewhere in the 
expression.

The warnings are very helpful in finding badly written expressions.

But why doesn't gcc warn about this code

  s.a = s.a++;
  p->a = p->a++;

IMHO, these lines also infringe upon the above mentioned rule or did I miss 
something important? 

Cheers
Daniel 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: gcc not warning on all occurences of operations with undefined results?
  2005-11-15 13:48 gcc not warning on all occurences of operations with undefined results? Daniel Kabs
@ 2005-11-15 16:03 ` John Love-Jensen
  2005-11-15 20:33 ` Ladislav Mecir
  1 sibling, 0 replies; 4+ messages in thread
From: John Love-Jensen @ 2005-11-15 16:03 UTC (permalink / raw)
  To: Daniel Kabs, MSX to GCC

Hi Daniel,

> IMHO, these lines also infringe upon the above mentioned rule or did I miss
> something important?

The compiler *CAN* warn about those kinds of infringements.

The compiler does *NOT* *HAVE* to warn about those kinds of infringements.

GCC 3.3.5 detects the simple case.  It does not (I presume) detect the
indirect case that you provided.

In some situations it may be very hard to detect the indirect case during
compile time, and it could be very expensive to check for it at run time.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: gcc not warning on all occurences of operations with undefined results?
  2005-11-15 13:48 gcc not warning on all occurences of operations with undefined results? Daniel Kabs
  2005-11-15 16:03 ` John Love-Jensen
@ 2005-11-15 20:33 ` Ladislav Mecir
  2005-11-16 11:13   ` Daniel Kabs
  1 sibling, 1 reply; 4+ messages in thread
From: Ladislav Mecir @ 2005-11-15 20:33 UTC (permalink / raw)
  To: Daniel Kabs; +Cc: gcc-help

Daniel Kabs napsal(a):

>Hello there,
>
>gcc 3.3.5 issues warnings on lines like
>
>  a[i] = i++;
>  i = i++;
>  *p++ = func(*p);
>
>e.g.
>"test_increment.c:19: warning: operation on `i' may be undefined"
>
>I take it, this is because of the C standard saying, that if I have an 
>expression and somewhere in that expression, I use an increment (decrement) 
>operator on a variable, I can not mention that variable elsewhere in the 
>expression.
>
>  
>
I think, that this is wrong. The exact rule should be more like "...if 
the result of the operation is not defined by standard..."

>The warnings are very helpful in finding badly written expressions.
>
>But why doesn't gcc warn about this code
>
>  s.a = s.a++;
>  p->a = p->a++;
>  
>
because in both these cases the result *is* defined

>IMHO, these lines also infringe upon the above mentioned rule or did I miss 
>something important? 
>
>Cheers
>Daniel 
>  
>
-L

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: gcc not warning on all occurences of operations with undefined results?
  2005-11-15 20:33 ` Ladislav Mecir
@ 2005-11-16 11:13   ` Daniel Kabs
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Kabs @ 2005-11-16 11:13 UTC (permalink / raw)
  To: Ladislav Mecir; +Cc: gcc-help

Hello Ladislav!

On Tuesday 15 November 2005 21:33, Ladislav Mecir wrote:
> >But why doesn't gcc warn about this code
> >
> >  s.a = s.a++;
> >  p->a = p->a++;
>
> because in both these cases the result *is* defined

I do not understand, why are those operations defined, but

i = i++;

is not? I fail to see the relevant difference in these assigments.

Thanks for your help,

Cheers
Daniel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-11-16 11:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-15 13:48 gcc not warning on all occurences of operations with undefined results? Daniel Kabs
2005-11-15 16:03 ` John Love-Jensen
2005-11-15 20:33 ` Ladislav Mecir
2005-11-16 11:13   ` Daniel Kabs

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).