Re: Const correctness
Re: Const correctness
- Subject: Re: Const correctness
- From: Cameron Hayne <email@hidden>
- Date: Mon, 6 Jun 2005 17:25:54 -0400
On 6-Jun-05, at 12:20 PM, Theodore H. Smith wrote:
I just realised, that C++'s upgrading of normal data, to const
data, actually defeats the entire purpose of having consts in the
first place.
char* noConstsAreBetter = "these consts suck\n";
const char* constsSuck = noConstsAreBetter;
printf( constsSuck );
noConstsAreBetter[0] = 'T'; // either constsSuck is altered, or
your app will crash.
printf( constsSuck );
That's one example of how you can alter const data, with perfectly
legal source code, that the compiler will not complain about.
I think you have misunderstood the purpose of 'const'. It does not
say that the data is invariant. It merely says that the variable
declared as 'const' cannot be used to change the data. It is of
course quite possible to change the data by using other, non-const
variables.
And the runtime crash in the above example has absolutely nothing to
do with 'const' and everything to do with trying to change an
embedded string.
I.e. the following crashes exactly the same:
char* noConstsAreBetter = "these consts suck\n";
printf( noConstsAreBetter );
noConstsAreBetter[0] = 'T';
printf( noConstsAreBetter );
--
Cameron Hayne
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden