public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Krishna Narayanan <krishnanarayanan132002@gmail.com>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: Doubts regarding the issue (bug 62181)
Date: Thu, 3 Feb 2022 20:47:57 +0000	[thread overview]
Message-ID: <CAH6eHdQvtV4nqk-FVx4EH7Twm8BM9wvfc=LjjoziabCBPcr+RQ@mail.gmail.com> (raw)
In-Reply-To: <CABhGnjtjJYk6o4_25jt27M++EGc3Fh_ZrS3B2ZM6EiTC95UMyg@mail.gmail.com>

On Thu, 3 Feb 2022 at 18:46, Krishna Narayanan via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> Respected Sir/Madam,
> I have been working on an issue (bug 62181) [C/C++] Expected new warning:
> "adding 'char' to a string does not append to the string"
> [-Wstring-plus-int].This is asking for a warning being added to gcc for
> such a case.Not using concat and indexing which were mentioned earlier as a
> solution to the warning I tried the example given in  https://godbolt.org/
> and was trying different permutations of addresses and pointers in clang
> and gcc (trunk), I am not sure but I tried '-w' in compiler flags and it
> worked fine for both clang and gcc ,it did not show any warning.I am not
> sure what exactly did the flag do but the warning arises in other
> optimizations.
> I had a doubt regarding the output of a slightly modified code in which I
> have taken a character instead of int ,
> #include <stdio.h>
>  char ch ='o';
> char bar()  {
> return 1;
>  }
> int foobar()  {
>  return ch; }
>  int main()
> {   const char* a = "aa";
>  const char *b = "bb" + bar();
> const char *c = "cc" + foobar();
>   printf("%s, %s, %s", a, b, c);
>  return 0 ;
> }
> In the output I get zR when I did it with gcc and get unicode when done
> with g++10.1 .I did not understand how does this output arise.Can you
> please help me out with this?

What do you mean you get "unicode"?

"bb" + bar() is equivalent to "bb" + (int)bar()
which is equivalent to "bb" + 1

"cc" + foobar() is equivalent to "cc" + (int)'o'
which has undefined behaviour, because it produces a pointer that is
outside the array of three chars "cc\0".
It will print whatever happens to be in memory approximately 100 bytes
after the "cc" string literal. That is random garbage of some kind.

The precise behaviour you get from this undefined behaviour is not
important, the point is that it's undefined behaviour, and that's why
a warning would be useful.

  reply	other threads:[~2022-02-03 20:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-03 18:45 Krishna Narayanan
2022-02-03 20:47 ` Jonathan Wakely [this message]
2022-02-04 11:35   ` Krishna Narayanan
2022-02-04 12:13     ` Jonathan Wakely
2022-02-04 16:24       ` Krishna Narayanan
2022-02-03 20:49 ` Jonathan Wakely

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='CAH6eHdQvtV4nqk-FVx4EH7Twm8BM9wvfc=LjjoziabCBPcr+RQ@mail.gmail.com' \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=krishnanarayanan132002@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).