Differences between debug and release versions-constant strings in C ++

Source: Internet
Author: User

See the following code:

 

Char g_array [] = "ABCD ";
Const char * g_pstring = "ABCD ";

Int main (INT argc, char * argv [], char * envp [])

{

G_array [0] = 'F ';
(Char *) g_pstring) [0] = 'F ';

Return 0;

}



 

An exception occurs at (char *) g_pstring) [0] = 'F';. Therefore, for constant strings, the compiler not only speaks during compilation

Legal

Check

,
It will automatically include
Set "read-only" protection for memory blocks.

 

Let's look at the following code:

Char * g_pstring = "ABCD ";

Int main (INT argc, char * argv [], char * envp [])

{


G_pstring [0] = 'F ';

Return 0;

}

You will also find exceptions at g_pstring [0] = 'F';, because strings are declared as constant strings in this way, the compiler will

Set "read-only" protection.

 

Supplement

2008-5-29

The compiler is only placed in the read-only data segment (. RDATA) in the debug version. For the release version, it is placed in the writable data segment (. Data). Therefore, there is no exception in rewriting the data in the release version. (This sentence has not been verified. Ant ~ Comment)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.