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


Groups > comp.lang.c > #104367

Re: modifying local const variable run time

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: modifying local const variable run time
Date 2017-02-22 10:16 -0500
Organization A noiseless patient Spider
Message-ID <o8k9s7$6o7$1@dont-email.me> (permalink)
References <c5e88b2a-20a5-4083-a40a-a17d57aa42f7@googlegroups.com>

Show all headers | View raw


On 02/22/2017 09:39 AM, rahul.dev.agg@gmail.com wrote:
> int func(void)
> {
>    const int i = getc();
> 
> }
> 
> Is this correct ? ...

Yes. It is permissible to initialize an object with a const-qualified type.

6.7.9p4 says "All the expressions in an initializer for an object that
has static or thread storage duration shall be constant expressions or
string literals.". getc() is a function call expression, and therefore
does not qualify as a constant expression (6.6p3), but 'i' has automatic
storage duration, so that requirement does not apply. I believe that in
C90, a similar requirement applied to all initializers, but that
requirement was restricted to static storage duration in C99, and
extended to thread storage duration when support for multi-threaded
programs was added in C2011.

> ... Can we modify const local variable run time ?

Any attempt to modify it after it has been initialized is either a
constraint violation or has undefined behavior.
The only operators that allow you to directly modify the value of an
object are the ++, --, simple assignment and compound assignment
operators. For all of those operators, it's a constraint violation if
the relevant operand is not a modifiable lvalue (6.5.2.4p1),
(6.5.3.1p1), 6.5.16p2). "A modifiable lvalue ... does not have a
const-qualified type..." (6.3.2.1p1). So, does this mean you can get
around that restriction by using type punning with an lvalue that isn't
const qualified? No:  "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." (6.7.3p6).
So, are you at least free to modify such objects by using calls to
standard library functions? That's covered too: "If an argument to a
function has an invalid value (such as ... a pointer to non-modifiable
storage when the corresponding parameter is not const-qualified) ... the
behavior is undefined." (7.1.4p1). None of the pointer parameters that
standard library functions write through are const-qualified.

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


Thread

modifying local const variable run time rahul.dev.agg@gmail.com - 2017-02-22 06:39 -0800
  Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 16:06 +0100
    Re: modifying local const variable run time rahul.dev.agg@gmail.com - 2017-02-22 07:26 -0800
      Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-22 16:31 +0100
        Re: modifying local const variable run time rahul.dev.agg@gmail.com - 2017-02-22 07:43 -0800
          Re: modifying local const variable run time Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-22 16:06 +0000
      Re: modifying local const variable run time James Kuyper <jameskuyper@verizon.net> - 2017-02-22 11:13 -0500
      Re: modifying local const variable run time supercat@casperkitty.com - 2017-02-22 08:34 -0800
        Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-22 23:03 +0100
      Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 17:34 +0100
        Re: modifying local const variable run time Robert Wessel <robertwessel2@yahoo.com> - 2017-02-22 10:52 -0600
          Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 17:52 +0100
            Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:33 -0800
            Re: modifying local const variable run time James Kuyper <jameskuyper@verizon.net> - 2017-02-22 15:50 -0500
              Re: modifying local const variable run time supercat@casperkitty.com - 2017-02-22 13:13 -0800
                Re: modifying local const variable run time Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-22 21:55 +0000
                Re: modifying local const variable run time supercat@casperkitty.com - 2017-02-22 14:43 -0800
        Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:23 -0800
          Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 18:32 +0100
            Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:50 -0800
              Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-22 23:07 +0100
                Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 15:20 -0800
                Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-23 09:50 +0100
  Re: modifying local const variable run time James Kuyper <jameskuyper@verizon.net> - 2017-02-22 10:16 -0500
  Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:17 -0800

csiph-web