const keyword

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

    const keyword

    I have my TC++ help file which has the following in it:

    const my_age = 39;
    _______________ ____________
    WARNING: A const variable can be indirectly modified by a
    pointer, as in
    the following:

    *(int *)&my_age = 35;

    When the const modifier is used with a pointer parameter in
    a function's
    parameter list, the function cannot modify the variable
    that the pointer
    points to. For example,

    int printf (const char *format, ...);

    Here the printf function is prevented from modifying the
    format string.
    _______________ __

    I can't figure out what they are trying to say.
    Could someone help?

    TIA.

    --
    main(){char s[40]="sbwjAeftqbnnf e/dpn!ps!CSbwjACj hgppu/dpn";
    int i;for(i=0;i<39; putchar(s[i++]-1));return 0;}
  • Josh Sebastian

    #2
    Re: const keyword

    On Sun, 12 Oct 2003 23:01:12 +0530, Ravi wrote:
    [color=blue]
    > I have my TC++ help file which has the following in it:
    >
    > const my_age = 39;
    > _______________ ____________
    > WARNING: A const variable can be indirectly modified by a
    > pointer, as in
    > the following:
    >
    > *(int *)&my_age = 35;[/color]

    They're saying that you can cast away const. This might or might not work
    all the time (it'll compile, but it might fail in strange ways at
    runtime). If your compiler documentation says you can do it, though, it
    should be safe enough with that compiler.
    [color=blue]
    > When the const modifier is used with a pointer parameter in
    > a function's
    > parameter list, the function cannot modify the variable
    > that the pointer
    > points to. For example,
    >
    > int printf (const char *format, ...);
    >
    > Here the printf function is prevented from modifying the
    > format string.[/color]

    They're saying that the string (the object format points to) is what's
    const, not format itself.

    int const* p; // *p is const
    int* const p; // p is const

    Josh

    Comment

    • osmium

      #3
      Re: const keyword

      Josh Sebastian writes:
      [color=blue]
      > On Sun, 12 Oct 2003 23:01:12 +0530, Ravi wrote:
      >[color=green]
      > > I have my TC++ help file which has the following in it:
      > >
      > > const my_age = 39;
      > > _______________ ____________
      > > WARNING: A const variable can be indirectly modified by a
      > > pointer, as in
      > > the following:
      > >
      > > *(int *)&my_age = 35;[/color]
      >
      > They're saying that you can cast away const. This might or might not work
      > all the time (it'll compile, but it might fail in strange ways at
      > runtime). If your compiler documentation says you can do it, though, it
      > should be safe enough with that compiler.[/color]

      With my compiler: no errors, no warnings, no work. The const was not
      changed.

      Borland TC++W 3.1


      Comment

      • Ravi

        #4
        Re: const keyword

        On Sun, 12 Oct 2003 13:34:08 -0700, "osmium"
        <r124c4u102@com cast.net> wrote:
        [color=blue]
        >With my compiler: no errors, no warnings, no work. The const was not
        >changed.
        >
        >Borland TC++W 3.1
        >[/color]

        Same here. But then why did they write that in the docs?

        --
        main(){char s[40]="sbwjAeftqbnnf e/dpn!ps!CSbwjACj hgppu/dpn";
        int i;for(i=0;i<39; putchar(s[i++]-1));return 0;}

        Comment

        • Rolf Magnus

          #5
          Re: const keyword

          osmium wrote:
          [color=blue]
          > Josh Sebastian writes:
          >[color=green]
          >> On Sun, 12 Oct 2003 23:01:12 +0530, Ravi wrote:
          >>[color=darkred]
          >> > I have my TC++ help file which has the following in it:
          >> >
          >> > const my_age = 39;
          >> > _______________ ____________
          >> > WARNING: A const variable can be indirectly modified by a
          >> > pointer, as in
          >> > the following:
          >> >
          >> > *(int *)&my_age = 35;[/color]
          >>
          >> They're saying that you can cast away const. This might or might not
          >> work all the time (it'll compile, but it might fail in strange ways
          >> at runtime). If your compiler documentation says you can do it,
          >> though, it should be safe enough with that compiler.[/color]
          >
          > With my compiler: no errors, no warnings, no work. The const was not
          > changed.[/color]

          That's the reason why you should avoid C style casts. You tell the
          compiler to be silent and do whatever it takes to convert to the
          specified type. You basically say "shut up, I know this doesn't make
          sense, but I want it anyway coz I know what I'm doing". Of course you
          _must_ actually know what you're doing.

          Comment

          • Micah Cowan

            #6
            Re: const keyword

            Rolf Magnus <ramagnus@t-online.de> writes:
            [color=blue]
            > osmium wrote:
            >[color=green]
            > > Josh Sebastian writes:
            > >[color=darkred]
            > >> On Sun, 12 Oct 2003 23:01:12 +0530, Ravi wrote:
            > >>
            > >> > I have my TC++ help file which has the following in it:
            > >> >
            > >> > const my_age = 39;
            > >> > _______________ ____________
            > >> > WARNING: A const variable can be indirectly modified by a
            > >> > pointer, as in
            > >> > the following:
            > >> >
            > >> > *(int *)&my_age = 35;
            > >>
            > >> They're saying that you can cast away const. This might or might not
            > >> work all the time (it'll compile, but it might fail in strange ways
            > >> at runtime). If your compiler documentation says you can do it,
            > >> though, it should be safe enough with that compiler.[/color]
            > >
            > > With my compiler: no errors, no warnings, no work. The const was not
            > > changed.[/color][/color]

            Perhaps the documentation is outdated; or perhaps they merely
            wanted to point out that it *might* work when executed on other
            platforms (of course, it might induce nasal daemons to fly out
            your nose, too ;-) )
            [color=blue]
            > That's the reason why you should avoid C style casts. You tell the
            > compiler to be silent and do whatever it takes to convert to the
            > specified type. You basically say "shut up, I know this doesn't make
            > sense, but I want it anyway coz I know what I'm doing". Of course you
            > _must_ actually know what you're doing.[/color]

            How would that be any less true using const_cast<> ?

            -Micah

            Comment

            Working...