Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const keyword

Reply
Thread Tools

const keyword

 
 
Ravi
Guest
Posts: n/a
 
      10-12-2003
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;}
 
Reply With Quote
 
 
 
 
Josh Sebastian
Guest
Posts: n/a
 
      10-12-2003
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      10-12-2003
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.

 
Reply With Quote
 
Ravi
Guest
Posts: n/a
 
      10-12-2003
On Sun, 12 Oct 2003 13:34:08 -0700, "osmium"
<(E-Mail Removed)> 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;}
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-12-2003
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


 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      10-12-2003
Rolf Magnus <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 1 09-06-2006 03:00 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 3 09-06-2006 02:58 PM



Advertisments