From: Gabriel Dos Reis <gdr@integrable-solutions.net>
To: Sriharsha <sriharsha.v@redpinesignals.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: assigning to const int via pointer
Date: Fri, 31 Dec 2004 11:08:00 -0000 [thread overview]
Message-ID: <m38y7ebue9.fsf@uniton.integrable-solutions.net> (raw)
In-Reply-To: <41D4F5F9.9000404@redpinesignals.com>
Sriharsha <sriharsha.v@redpinesignals.com> writes:
| Ian Lance Taylor wrote:
|
| >Joe Steeve <joe_steeve@gmx.net> writes:
| >
| >
| >>The following code assigns a value to a `const int` via a
| >>pointer.,
| >>
| >>#include <stdio.h>
| >>
| >> int main()
| >>{
| >> const int x=5;
| >> int *ptr;
| >> ptr = &x;
| >> *ptr = 10;
| >> printf("%d",x);
| >>}
| >>
| >>The code gives `10` for the following compilation
| >>
| >> $gcc -o test test.c
| >>
| >>It gives `5` when using optimisations switches.,
| >>
| >> $gcc -o test -O2 test.c
| >>
| >> Feature or bug or any explanation for this?
| >
| >When you declare that the variable is const, you are declaring that
| >the value does not change. When you do change it, you are using
| >undefined behaviour. When the compiler sees undefined behaviour, it
| >does not behave predictably.
| >
| >ISO C99 6.7.3: "If an attempt is made to modify an object defined with
| >a const-qualified type through use of an lvalue with
| >non-const-qualified type, the behavior is undefined."
| >
| I am a little confused. The rule says:
| If an attempt is made to modify an object, defined with a
| const-qualified type, through use of an lvalue with a
| non-const-qualified type, the behavior is undefined.
|
| Now, in the above, program, we are not trying to alter the value of
| the variable x as follows:
| x = 10;
|
| But we are trying to alter the contents of a memory location, which
| happens to be where the variable 'x' refers to, by using a pointer,
| which is defined behaviour.
which is precisely what the rule outlaws.
-- Gaby
next prev parent reply other threads:[~2004-12-31 11:08 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-12-30 6:27 Joe Steeve
2004-12-30 7:19 ` Sriharsha
2004-12-30 16:02 ` Ian Lance Taylor
2004-12-31 6:44 ` Sriharsha
2004-12-31 11:08 ` Gabriel Dos Reis [this message]
2004-12-31 14:52 ` Ian Lance Taylor
2005-01-03 6:57 ` Sriharsha
2004-12-31 21:43 ` Joe Steeve
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=m38y7ebue9.fsf@uniton.integrable-solutions.net \
--to=gdr@integrable-solutions.net \
--cc=gcc-help@gcc.gnu.org \
--cc=sriharsha.v@redpinesignals.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).