From: Nicholas Mc Guire <der.herr@hofr.at>
To: vijay nag <vijunag@gmail.com>
Cc: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Re: Crazy compiler optimization
Date: Wed, 09 Oct 2013 10:18:00 -0000 [thread overview]
Message-ID: <20131009101838.GC30167@opentech.at> (raw)
In-Reply-To: <CAKhyrx_1m0K868TVXg8kwo+EhbB+VFV0RkPqDHJ+FA8BV4w4Gw@mail.gmail.com>
On Wed, 09 Oct 2013, vijay nag wrote:
> Hello GCC,
>
> I'm facing a wierd compiler optimization problem. Consider the code
> snippet below
>
> #include <stdio.h>
>
> int printChar(unsigned long cur_col, unsigned char c)
> {
> char buf[256];
> char* bufp = buf;
> char cnt = sizeof(buf) - 2; /* overflow in implicit type conversion */
> unsigned long terminal_width = 500;
>
> while ((cur_col++ < terminal_width) && cnt) {
> *bufp++ = c;
> cnt--;
> }
>
> *bufp++ = '\n';
> *bufp = 0;
>
> printf("%c\n", buf[0]);
> return 1;
> }
>
> int main()
> {
> printChar(80, '-');
> return 1;
> }
>
> While compiler optimization should guarantee that the result of
> execution is same at all optimization levels, I'm observing difference
> in the result of execution of the above program when optimized to
> different levels. Although there is fundamental problem with the
> statement "char cnt = sizeof(buf) - 2", GCC seems to be warning(that
> too only when -pedantic flag is used) about overflow error while
> silently discarding any code related to cnt i.e. the check "&& cnt" in
> the if-clause is silently discarded by the compiler at -O2.
>
> $]gcc -g char.c -o char.c.unoptimized -m32 -O0 -Wall -Wextra -pedantic
> char.c: In function ?printChar?:
> char.c:8: warning: overflow in implicit constant conversion
>
This compiler optimization dependency is visible with quite a few code examples
that violate the C standard.
Integer overflow/underflow results in undefined behavior - you are in the
wild lands basically - you should not expect C-standard violations to result
in "reliable undefined" code.
See C99 Annex J.2 for details of undefined behaviors.
thx!
hofrat
next prev parent reply other threads:[~2013-10-09 10:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-09 9:36 vijay nag
2013-10-09 9:54 ` Jonathan Wakely
2013-10-09 10:02 ` vijay nag
2013-10-09 10:16 ` Jonathan Wakely
2013-10-09 15:40 ` David Brown
2013-10-09 10:18 ` Nicholas Mc Guire [this message]
2013-10-09 17:48 ` Ian Lance Taylor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20131009101838.GC30167@opentech.at \
--to=der.herr@hofr.at \
--cc=gcc-help@gcc.gnu.org \
--cc=vijunag@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).