Text Constant Area in C ++

Source: Internet
Author: User

If changed:

Then it runs correctly. After thinking about it, I finally found the answer by checking the information in the morning.

I made a test program and cut a graph. The answer is as follows:

All the answers are on the graph.

Summary:

1. char * p = "hello"; // determine the content of the string during compilation.

Char q [] = "hello"; // determine the string content at runtime

Where, p Stores the string address, and q stores the string itself. Therefore, p cannot be modified after initialization.

In terms of speed, it is obvious that the second method is faster than the first one.

2. C language program memory allocation, which has the following areas:

Like char * p = "hello"; strings defined in this way are placed in the text constant area. Therefore, it cannot be modified after definition.

Ps: url: http://hi.baidu.com/elnydtrjwrafkmr/item/49d767db2ad476f192a974d5

Ps:

String str = 2 + "abcdef"; // (here, the + time arithmetic operator is not the one after the overload)
Cout <"The str7 is \" "<str7 <" \ "" <endl;

The answer is The str7 is "cdef ".

Analysis: All constants in the program exist in the constant area. "Abcdef" is stored in the text constant area and has a relative offset address. Is a part of the constant area. If a value is added to the string operation, the result is equivalent to the original offset, the returned string is a new string starting from the new offset address to '\ 0.

Related Article

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.