const keyword

Discussion in 'C++' started by Ravi, Oct 12, 2003.

  1. Ravi

    Ravi Guest

    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]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
    int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
     
    Ravi, Oct 12, 2003
    #1
    1. Advertising

  2. 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.

    > 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.


    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
     
    Josh Sebastian, Oct 12, 2003
    #2
    1. Advertising

  3. Ravi

    Rolf Magnus Guest

    osmium wrote:

    > Josh Sebastian writes:
    >
    >> 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.

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


    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.
     
    Rolf Magnus, Oct 12, 2003
    #3
  4. Ravi

    Ravi Guest

    On Sun, 12 Oct 2003 13:34:08 -0700, "osmium"
    <> wrote:

    >With my compiler: no errors, no warnings, no work. The const was not
    >changed.
    >
    >Borland TC++W 3.1
    >


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

    --
    main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
    int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
     
    Ravi, Oct 12, 2003
    #4
  5. Ravi

    osmium Guest

    Josh Sebastian writes:

    > 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.


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

    Borland TC++W 3.1
     
    osmium, Oct 12, 2003
    #5
  6. Ravi

    Micah Cowan Guest

    Rolf Magnus <> writes:

    > osmium wrote:
    >
    > > Josh Sebastian writes:
    > >
    > >> 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.

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


    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 ;-) )

    > 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.


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

    -Micah
     
    Micah Cowan, Oct 12, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. coala
    Replies:
    3
    Views:
    384
    coala
    Sep 6, 2006
  2. coala
    Replies:
    1
    Views:
    598
    Victor Bazarov
    Sep 6, 2006
  3. Replies:
    11
    Views:
    1,124
  4. Javier
    Replies:
    2
    Views:
    584
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,142
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page