constants - pp vs. const

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • copx

    constants - pp vs. const

    I remember reading somewhere that C's 'const' keyword is almost useless,
    except for maybe triggering some additional compiler warnings. 'Useless' as
    in: a conforming C compiler cannot assume that a variable declared const is
    actually constant (never modified), because it is valid to cast the const
    modifier away e.g.

    const int foo = 2;
    int *bar;

    bar = (int *)&foo;

    *bar = 4;

    Is that correct?



  • copx

    #2
    Re: constants - pp vs. const


    "copx" <copx@gazeta.pl schrieb im Newsbeitrag
    news:f4jokq$j69 $1@inews.gazeta .pl...
    >I remember reading somewhere that C's 'const' keyword is almost useless,
    [snip]

    Sorry, wrong header. I originally intended to post a different question and
    forgot to change the subject.


    Comment

    • Eric Sosman

      #3
      Re: constants - pp vs. const

      copx wrote On 06/11/07 11:10,:
      I remember reading somewhere that C's 'const' keyword is almost useless,
      except for maybe triggering some additional compiler warnings. 'Useless' as
      in: a conforming C compiler cannot assume that a variable declared const is
      actually constant (never modified), because it is valid to cast the const
      modifier away e.g.
      >
      const int foo = 2;
      int *bar;
      >
      bar = (int *)&foo;
      >
      *bar = 4;
      >
      Is that correct?
      No, any attempt to modify a const-qualified variable
      produces undefined behavior. As a practical matter, you
      will find that some compilers allocate const-qualified
      variables in read-only memory.

      As for the "almost useless" part, I refuse to take
      part in empty-headed debates with scurrilous morons who
      use loaded terms to advance their impious agendas.

      --
      Eric.Sosman@sun .com

      Comment

      • Richard Bos

        #4
        Re: constants - pp vs. const

        "copx" <copx@gazeta.pl wrote:
        I remember reading somewhere that C's 'const' keyword is almost useless,
        except for maybe triggering some additional compiler warnings. 'Useless' as
        in: a conforming C compiler cannot assume that a variable declared const is
        actually constant (never modified), because it is valid to cast the const
        modifier away e.g.
        >
        const int foo = 2;
        int *bar;
        >
        bar = (int *)&foo;
        >
        *bar = 4;
        >
        Is that correct?
        Not quite. Doing so causes undefined behaviour. A C implementation _is_
        allowed to assume that a const _object_ remains constant. What you're
        thinking of is the other end of the equation: given

        const int *bar;

        an implementation cannot assume that the object at bar will remain
        constant, because bar can be pointed at an object that itself is not
        const. IOW, _pointers to_ const types are not as useful as they could
        be. However, they're still very useful for function parameters; for
        example, given the declaration

        void func(const int *baz);

        both the implementation and the maintenance programmer can rely on
        func() not changing the object that baz points at, at least not through
        baz, and provided func() is well written. This is frequently used in the
        Standard library; for example, strcpy() is declared

        char *strcpy(char *s1, const char *s2);

        and this means that if you call strcpy() correctly, you can rely on it
        never modifying the second string you pass it, whatever it does to the
        first string. (In C99, restrict is used to tighten both that "if" and
        the resulting guarantee.)

        Richard

        Comment

        • copx

          #5
          Re: constants - pp vs. const


          "Richard Bos" <rlb@hoekstra-uitgeverij.nlsc hrieb im Newsbeitrag
          news:466d66c5.2 79873005@news.x s4all.nl...
          "copx" <copx@gazeta.pl wrote:
          >
          >I remember reading somewhere that C's 'const' keyword is almost useless,
          >except for maybe triggering some additional compiler warnings. 'Useless'
          >as
          >in: a conforming C compiler cannot assume that a variable declared const
          >is
          >actually constant (never modified), because it is valid to cast the const
          >modifier away e.g.
          >>
          >const int foo = 2;
          >int *bar;
          >>
          >bar = (int *)&foo;
          >>
          >*bar = 4;
          >>
          >Is that correct?
          >
          Not quite. Doing so causes undefined behaviour. A C implementation _is_
          allowed to assume that a const _object_ remains constant. What you're
          thinking of is the other end of the equation: given
          >
          const int *bar;
          >
          an implementation cannot assume that the object at bar will remain
          constant, because bar can be pointed at an object that itself is not
          const.
          [snip]

          Thank for you for clearing this up.





          Comment

          • copx

            #6
            Re: constants - pp vs. const


            "Eric Sosman" <Eric.Sosman@su n.comschrieb im Newsbeitrag
            news:1181575088 .964548@news1nw k...
            [snip]
            No, any attempt to modify a const-qualified variable
            produces undefined behavior. As a practical matter, you
            will find that some compilers allocate const-qualified
            variables in read-only memory.
            Ok, thanks.
            As for the "almost useless" part, I refuse to take
            part in empty-headed debates with scurrilous morons who
            use loaded terms to advance their impious agendas.
            Loaded terms like "useless" as opposed to technical terms like "scurrilous
            morons", I guess :P


            Comment

            • Martin Ambuhl

              #7
              Re: constants - pp vs. const

              copx wrote:
              "Eric Sosman" <Eric.Sosman@su n.comschrieb im Newsbeitrag
              news:1181575088 .964548@news1nw k...
              > As for the "almost useless" part, I refuse to take
              >part in empty-headed debates with scurrilous morons who
              >use loaded terms to advance their impious agendas.
              >
              Loaded terms like "useless" as opposed to technical terms like "scurrilous
              morons", I guess :P
              SWOOSH!

              Comment

              • Christopher Benson-Manica

                #8
                Re: constants - pp vs. const

                Richard Bos <rlb@hoekstra-uitgeverij.nlwr ote:
                A C implementation _is_ allowed to assume that a const _object_ remains
                constant.
                ITYM "program" rather than "implementation ". The "as-if" rule
                applies, of course.

                --
                C. Benson Manica | I *should* know what I'm talking about - if I
                cbmanica(at)gma il.com | don't, I need to know. Flames welcome.

                Comment

                • Keith Thompson

                  #9
                  Re: constants - pp vs. const

                  Christopher Benson-Manica <ataru@vinland. freeshell.orgwr ites:
                  Richard Bos <rlb@hoekstra-uitgeverij.nlwr ote:
                  >A C implementation _is_ allowed to assume that a const _object_ remains
                  >constant.
                  >
                  ITYM "program" rather than "implementation ". The "as-if" rule
                  applies, of course.
                  No, I think he meant implementation. More specifically, an
                  implementation is allowed to assume that a const object is not
                  modified, and to generate code that depends on that assumption.

                  For example, given:

                  #include <stdio.h>
                  int main(void)
                  {
                  const int x = 42;
                  *(int*)&x = 123;
                  printf("x = %d\n", x); /* line 6 */
                  return 0;
                  }

                  the compiler is allowed to replace the reference to x on line 6 with a
                  literal 42. At least one compiler actually does this if you request
                  optimization.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • CBFalconer

                    #10
                    Re: constants - pp vs. const

                    copx wrote:
                    >
                    I remember reading somewhere that C's 'const' keyword is almost
                    useless, except for maybe triggering some additional compiler
                    warnings. 'Useless' as in: a conforming C compiler cannot assume
                    that a variable declared const is actually constant (never
                    modified), because it is valid to cast the const modifier away
                    e.g.
                    >
                    const int foo = 2;
                    int *bar;
                    >
                    bar = (int *)&foo;
                    >
                    *bar = 4;
                    >
                    Is that correct?
                    Correct, but foolish. Use it properly.

                    --
                    <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                    <http://www.securityfoc us.com/columnists/423>
                    <http://www.aaxnet.com/editor/edit043.html>
                    <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                    cbfalconer at maineline dot net



                    --
                    Posted via a free Usenet account from http://www.teranews.com

                    Comment

                    • Keith Thompson

                      #11
                      Re: constants - pp vs. const

                      CBFalconer <cbfalconer@yah oo.comwrites:
                      copx wrote:
                      >I remember reading somewhere that C's 'const' keyword is almost
                      >useless, except for maybe triggering some additional compiler
                      >warnings. 'Useless' as in: a conforming C compiler cannot assume
                      >that a variable declared const is actually constant (never
                      >modified), because it is valid to cast the const modifier away
                      >e.g.
                      >>
                      >const int foo = 2;
                      >int *bar;
                      >>
                      >bar = (int *)&foo;
                      >>
                      >*bar = 4;
                      >>
                      >Is that correct?
                      >
                      Correct, but foolish. Use it properly.
                      No, it's not correct. Attempting to modify a const-qualified object
                      invokes undefined behavior.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • Christopher Benson-Manica

                        #12
                        Re: constants - pp vs. const

                        Keith Thompson <kst-u@mib.orgwrote:
                        Richard Bos <rlb@hoekstra-uitgeverij.nlwr ote:
                        A C implementation _is_ allowed to assume that a const _object_ remains
                        constant.
                        No, I think he meant implementation. More specifically, an
                        implementation is allowed to assume that a const object is not
                        modified, and to generate code that depends on that assumption.
                        On another reading, I think you're right, but the phrasing still seems
                        a bit unclear to me. Just another Monday I suppose.

                        --
                        C. Benson Manica | I *should* know what I'm talking about - if I
                        cbmanica(at)gma il.com | don't, I need to know. Flames welcome.

                        Comment

                        • Marc Boyer

                          #13
                          Re: constants - pp vs. const

                          Le 11-06-2007, copx <copx@gazeta.pl a écrit :
                          I remember reading somewhere that C's 'const' keyword is almost useless,
                          except for maybe triggering some additional compiler warnings. 'Useless' as
                          in: a conforming C compiler cannot assume that a variable declared const is
                          actually constant (never modified), because it is valid to cast the const
                          modifier away e.g.
                          >
                          const int foo = 2;
                          int *bar;
                          >
                          bar = (int *)&foo;
                          You are lying to the compiler.
                          Try
                          char tab[9]={0,1,2,3,4,5,6 ,7,8};
                          double *d= (double*) (tab+1);
                          what happen on your platform ?
                          *bar = 4;
                          >
                          Is that correct?
                          No. Writing in a const variable is an undefined behavior. In
                          a PC-like environment, it could be as if the variable was not
                          const, but the compiler is also allowed to put the const varible
                          in a read-only memory.

                          Marc Boyer

                          Comment

                          Working...