public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Utkarsh Singh <utkarsh190601@gmail.com>
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>, Aakarsh MJ <aakarshmj248@gmail.com>
Subject: Re: Problems in array access
Date: Tue, 31 Aug 2021 14:09:37 +0530	[thread overview]
Message-ID: <87lf4ic8ja.fsf@gmail.com> (raw)
In-Reply-To: <CAH6eHdSRZ2yLHzpmpozdhPjhP480CyJ-4Ft3qyBcEUYymTZD3g@mail.gmail.com>

On 2021-08-31, 09:28 +0100, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:

> On Tue, 31 Aug 2021 at 09:11, Utkarsh Singh wrote:
>>
>> Hello GCC mailing list,
>>
>> In one of my friend's C programming class, they asked him a question on
>> the topic of array bounds based on the follwing code snippet:
>>
>> #include <stdio.h>
>>
>> int main(void)
>> {
>>         char str[] = {'G' , 'C' , 'C' };
>>         str[3] = '\0' ; /* Isn't this invalid? */
>>         printf("%s\n", str);
>> }
>>
>> In an ideal case, str[3] should be a case of out-of-bound array access.
>> But when compiling the above with -Wall option flag GCC shows no
>> warning.  So, am I missing something?
>
> This question belongs on the gcc-help mailing list, not here.

Sorry! I will keep this in mind.

> The code has undefined behaviour.
>
> Some GCC warnings depend on checks done during optimization. GCC will
> warn about this code if you use -Wall -O2 and you will get a runtime
> error if you compile with -fsanitize=undefined

Great! And thank you for a quick reply.

-- 
Utkarsh Singh
http://utkarshsingh.xyz

  reply	other threads:[~2021-08-31  8:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-31  8:10 Utkarsh Singh
2021-08-31  8:28 ` Jonathan Wakely
2021-08-31  8:39   ` Utkarsh Singh [this message]
2021-08-31 15:08     ` Martin Sebor
2021-09-01  2:31       ` Utkarsh Singh

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=87lf4ic8ja.fsf@gmail.com \
    --to=utkarsh190601@gmail.com \
    --cc=aakarshmj248@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jwakely.gcc@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).