Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #5878

Re: a doubt about c

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: a doubt about c
Date 2011-06-11 20:24 -0700
Organization None to speak of
Message-ID <lnhb7v91g9.fsf@nuthaus.mib.org> (permalink)
References (3 earlier) <slrniv7knq.s0f.angel+news@pearlgates.net> <95i37sFrhnU3@mid.individual.net> <qiv7v6d8p89b7qkmfcmg2bhrq0qg48sim1@4ax.com> <lnr5707y9f.fsf@nuthaus.mib.org> <it17c7$6va$1@dont-email.me>

Show all headers | View raw


Stephen Sprunk <stephen@sprunk.org> writes:
> On 11-Jun-11 18:18, Keith Thompson wrote:
>> DSF <notavalid@address.here> writes:
>>>   May I ask why you feel that way?  #define constants certainly have a
>>> potential performance benefit over a const variable.  The use of a
>>> #define constant is often replaced with an immediate value, whereas a
>>> const variable is likely to be stored in a static memory pool which is
>>> often part of the program itself.  This leaves the potential for
>>> slower execution and a larger program size.
>> 
>> If a const object's initialization is a constant expression, any
>> decent optimizing compiler will replace any reference to the object
>> with its value (unless fetching it from memory is actually faster
>> on the system in question).
>
> AFAICT, that would not be legal since some other code outside the
> current compilation unit could cast away the variable's const-ness and
> alter its value.  It's also not possible to eliminate the object
> entirely if some code takes its address with the & operator.

It's not possible to change the value of a const-qualified object
without invoking undefined behavior, when means that the compiler
is permitted to assume that you haven't done that.

You're right that the object can't be eliminated if its address is used
-- but the compiler can still replace a reference to its value with the
actual initial value.

> If const objects were actually constant expressions in C rather than
> merely read-only objects, as one might naïvely expect (and they are in
> C++), that'd be a different matter.

I've just posted to comp.std.c suggesting such a change.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

a doubt about c Ya Shou <zxyzxy12321@gmail.com> - 2011-06-11 06:11 -0700
  Re: a doubt about c James Kuyper <jameskuyper@verizon.net> - 2011-06-11 09:36 -0400
  Re: a doubt about c Angel <angel+news@spamcop.net> - 2011-06-11 14:37 +0000
    Re: a doubt about c Keith Thompson <kst-u@mib.org> - 2011-06-11 11:55 -0700
      Re: a doubt about c Angel <angel+news@spamcop.net> - 2011-06-11 20:41 +0000
        Re: a doubt about c Ian Collins <ian-news@hotmail.com> - 2011-06-12 08:58 +1200
          Re: a doubt about c Shao Miller <sha0.miller@gmail.com> - 2011-06-11 17:09 -0500
          Re: a doubt about c Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-11 18:25 -0400
            Re: a doubt about c Ian Collins <ian-news@hotmail.com> - 2011-06-12 11:43 +1200
            Re: a doubt about c Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-06-12 05:38 -0700
              Re: a doubt about c Roberto Waltman <usenet@rwaltman.com> - 2011-06-12 10:12 -0400
                Re: a doubt about c Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-12 10:59 -0400
                Re: a doubt about c Nobody <nobody@nowhere.com> - 2011-06-15 17:07 +0100
                Re: a doubt about c Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-06-12 11:33 -0700
                Re: a doubt about c James Kuyper <jameskuyper@verizon.net> - 2011-06-15 09:53 -0400
                Re: a doubt about c Dr Nick <3-nospam@temporary-address.org.uk> - 2011-06-16 18:46 +0100
          Re: a doubt about c DSF <notavalid@address.here> - 2011-06-11 18:53 -0500
            Re: a doubt about c Keith Thompson <kst-u@mib.org> - 2011-06-11 16:18 -0700
              Re: a doubt about c Stephen Sprunk <stephen@sprunk.org> - 2011-06-11 21:13 -0500
                Re: a doubt about c Ian Collins <ian-news@hotmail.com> - 2011-06-12 14:56 +1200
                Re: a doubt about c Keith Thompson <kst-u@mib.org> - 2011-06-11 20:24 -0700
                Re: a doubt about c Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-12 08:10 -0400
            Re: a doubt about c Ian Collins <ian-news@hotmail.com> - 2011-06-12 11:49 +1200
        Re: a doubt about c Keith Thompson <kst-u@mib.org> - 2011-06-11 15:36 -0700
      Re: a doubt about c Nobody <nobody@nowhere.com> - 2011-06-12 00:23 +0100
        Re: a doubt about c Seebs <usenet-nospam@seebs.net> - 2011-06-13 14:39 +0000
          Re: a doubt about c Squeamizh <squeamz@hotmail.com> - 2011-06-15 15:44 -0700
            Re: a doubt about c Geoff <geoff@invalid.invalid> - 2011-06-15 19:57 -0700
              Re: a doubt about c "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-16 18:39 +0100
                Re: a doubt about c Nobody <nobody@nowhere.com> - 2011-06-17 15:39 +0100
                Re: a doubt about c Willem <willem@toad.stack.nl> - 2011-06-17 14:49 +0000
                Re: a doubt about c Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-17 09:14 -0600
                Re: a doubt about c Shao Miller <sha0.miller@gmail.com> - 2011-06-17 12:00 -0500
                Re: a doubt about c Shao Miller <sha0.miller@gmail.com> - 2011-06-17 11:08 -0500
                Re: [OT] Doubt vs. question. (Re: a doubt about c) Shao Miller <sha0.miller@gmail.com> - 2011-06-17 13:00 -0500
                Re: [OT] Doubt vs. question. (Re: a doubt about c) Shao Miller <sha0.miller@gmail.com> - 2011-06-22 19:01 -0500
                Re: a doubt about c Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-17 09:14 -0600
                Re: a doubt about c Seebs <usenet-nospam@seebs.net> - 2011-06-17 22:16 +0000
                Re: a doubt about c "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-18 02:24 +0100
      Re: a doubt about c Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-11 23:09 -0600
      [OT] Doubt vs. question.  (Re: a doubt about c) Roberto Waltman <usenet@rwaltman.com> - 2011-06-12 10:05 -0400
        Re: [OT] Doubt vs. question.  (Re: a doubt about c) Keith Thompson <kst-u@mib.org> - 2011-06-12 11:22 -0700
          Re: [OT] Doubt vs. question.  (Re: a doubt about c) Ben Pfaff <blp@cs.stanford.edu> - 2011-06-12 11:44 -0700
        Re: [OT] Doubt vs. question.  (Re: a doubt about c) Ralph Spitzner <rasp@spitzner.org> - 2011-06-13 11:31 +0200
          Re: [OT] Doubt vs. question.  (Re: a doubt about c) James Kuyper <jameskuyper@verizon.net> - 2011-06-15 10:03 -0400
            Re: [OT] Doubt vs. question. (Re: a doubt about c) Dr Nick <3-nospam@temporary-address.org.uk> - 2011-06-17 07:52 +0100
            Re: [OT] Doubt vs. question.  (Re: a doubt about c) Michael Press <rubrum@pacbell.net> - 2011-06-19 00:41 -0700
              Re: [OT] Doubt vs. question.  (Re: a doubt about c) James Kuyper <jameskuyper@verizon.net> - 2011-06-19 07:04 -0400
                Re: [OT] Doubt vs. question.  (Re: a doubt about c) Bill Waddington <william.waddington@beezmo.com> - 2011-06-23 11:03 -0700
  Re: a doubt about c Shao Miller <sha0.miller@gmail.com> - 2011-06-11 11:23 -0500
    Re: a doubt about c pete <pfiland@mindspring.com> - 2011-06-11 13:56 -0400
      Re: a doubt about c "dr.oktopus" <blindwilly@freeonline.zzn.com> - 2011-06-11 12:53 -0700
        Re: a doubt about c Lew Pitcher <lpitcher@teksavvy.com> - 2011-06-11 16:01 -0400
        Re: a doubt about c Keith Thompson <kst-u@mib.org> - 2011-06-11 13:07 -0700
        Re: a doubt about c Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-11 14:13 -0600
          Re: a doubt about c Nobody <nobody@nowhere.com> - 2011-06-11 23:31 +0100
            Re: a doubt about c pete <pfiland@mindspring.com> - 2011-06-11 20:07 -0400
            Re: a doubt about c Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-11 23:05 -0600
      Re: a doubt about c Shao Miller <sha0.miller@gmail.com> - 2011-06-11 16:42 -0500
        Re: a doubt about c "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-06-11 14:23 -0700
          Re: a doubt about c Shao Miller <sha0.miller@gmail.com> - 2011-06-11 17:27 -0500
            Re: a doubt about c Keith Thompson <kst-u@mib.org> - 2011-06-11 15:42 -0700
  Re: a doubt about c Seebs <usenet-nospam@seebs.net> - 2011-06-13 14:39 +0000

csiph-web