linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Steven Smith" <sos22@cam.ac.uk>
To: Nasir Simbolon <nasir@orbisindonesia.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: constant variable
Date: Thu, 11 Mar 2004 13:42:15 +0000	[thread overview]
Message-ID: <20040311134215.GA4714@archibold.chu.cam.ac.uk> (raw)
In-Reply-To: <200403111819.00752.nasir@orbisindonesia.com>

[-- Attachment #1: Type: text/plain, Size: 798 bytes --]

>  	const int i=9;
>      	int* j=NULL;
> 	 j = (int*) &i;
>  	*j = 3;

> when I compile using gcc version 3.3.2 (Debian) and run, I have the result :
> 
> value of i = 9
> value of *j = 3
> 
> Now, I notice  that j point to address of i BUT they do not  have the same 
> value.

Probably because of compiler optimisations.  If you've specified that
something is const, then the compiler is perfectly at liberty to
assume that it is.  This means that it can assume the initial value of
i still applies, and so, rather than going to memory for i, it can
just substitute the constant 9 in.  This is likely to be much quicker
at run time.

I can only reproduce this behaviour if I have compiler optimisations
turned on, which adds some more weight to the explanation.

Steven.

[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]

  reply	other threads:[~2004-03-11 13:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-11 11:19 constant variable Nasir Simbolon
2004-03-11 13:42 ` Steven Smith [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-03-11 13:28 Sandro Dangui

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=20040311134215.GA4714@archibold.chu.cam.ac.uk \
    --to=sos22@cam.ac.uk \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=nasir@orbisindonesia.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 NNTP newsgroup(s).