• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Const correctness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
  • Follow-Ups:
    • Re: Const correctness
      • From: Keith Ray <email@hidden>
References: 
 >Const correctness (From: "Theodore H. Smith" <email@hidden>)

  • Prev by Date: Xcode 2.1 Available
  • Next by Date: New xcodeproj file in Subversion SCM
  • Previous by thread: Const correctness
  • Next by thread: Re: Const correctness
  • Index(es):
    • Date
    • Thread