C ++ string type string Storage Method

Source: Internet
Author: User

I wonder if you have read the member data of the string type variable. IN THE STRING variable, it seems that the string is actually stored as a pointer named _ PTR, which points to the first address of the string stored by string. When the string variable is defined, if it is not initialized, _ PTR is a pointer pointing to null, and _ PTR looks like a const char * type pointer. If it is an empty string, you cannot use a value assignment statement like STR [0] = 'w' (assuming STR is of the string type ).

The problem is as follows:

1) When we assign values to the string variable, does it store strings by dynamically allocating memory to _ PTR or by other means?

2) If we store strings in other ways, how much storage space does the system provide for the string variable?

3) is the storage space on the stack or on the stack? If a large number of string variables are defined, will they occupy too much storage space and cause a great waste of storage space?


The result is as follows:

Char * PTR; // memory pointer, used to save strings
Size_t cap; // The length of the dynamic memory
Size_t Len; // String Length
The actual implementation will be more complex, and it is a template.

When assigning values
Check whether PTR is null. If so, allocate enough dynamic memory.
If PTR is not null, check whether cap is enough to put down the string. If not, re-allocate the memory. Then call functions such as STD: copy or strcpy to copy the string to PTR.

(1) Does it store strings by dynamically allocating memory to _ PTR, or in other ways?
Dynamic memory.

(2) How much storage space does the system provide for string variables?
This depends on the specific implementation Definition. Different implementations are not the same, but generally, only the necessary memory is allocated, that is, strings like "ASDF" are allocated with only five char characters.

(3) is the storage space on the stack or on the stack?
Heap

(4) If many string variables are defined, will they occupy too much storage space and cause a great waste of storage space?
No. The string and vector are different. Generally, the memory is not reserved for the element and there is no waste.

 

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.