STD: Special for string memory management. Pay attention to this when using multiple threads.

Source: Internet
Author: User

From: http://blog.csdn.net/zhihuizhilv/article/details/5388946

Vc2003 DevelopmentProgram, Encountered a crash problem, after a hard query, found that and STD: String memory management and their usage.

STD: String uses a lazy memory management policy. For example:

String str1 = "sdsada ";

String str2 = str1;

At this time, the two objects refer to the same memory string. That is to say, when str2 is constructed through str1, str2 does not immediately allocate a piece of its own string memory, but directly uses str1, and adds the reference number of this memory to 1.

What is incredible is that STD: String does not consider the concurrency of multithreading, which may cause problems when operating string objects with shared string memory among multiple threads.

 

To avoid this risk, we can only ensure that the string objects operated by multiple threads do not share the string memory. AboveCodeIs:

String str2 = str1.c _ STR ();

Can solve the problem.

You can also use cstring to skip this issue.

 

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.