Cstring analysis and explanation

Source: Internet
Author: User
1 cstring Implementation Mechanism Cstring manages strings through "reference", such as window kernel objects and COM objects. Cstring also uses this mechanism to manage allocated memory blocks. In fact, the cstring object only has one pointer member variable, so the length of any cstring instance is only 4 bytes. because of this, such a memory block can be referenced by multiple cstrings. how many cstrings reference it? It also records some information. Such as the number of referenced items, string length, and memory allocation length. The structure of the referenced memory block is defined as follows: struct cstringdata {long nrefs; // indicates how many cstrings reference it int ndatalength; // the actual length of the string. int nalloclength; // The total length of memory allocated (excluding the 12 bytes in this header)}; with this information, cstring can correctly allocate, manage, and release referenced memory blocks. If you want to obtain this information during program debugging. You can enter the following expression in the watch window: (cstringdata *) (This-> m_pchdata)-1) or (cstringdata *) (Str. m_pchdata)-1) // STR indicates that cstring instances are not only efficient, but also have less memory allocated when copying a large number of cstring instances. 2. lpctstr and getbuffer (INT nminbuflength)These two functions provide compatible conversions with standard C. The two functions actually return pointers. (1) The execution process of the lpctstr function is actually very simple. It only returns the string address that references the memory block. It is provided as an operator overload, so sometimes it can be implicitly converted in code, but sometimes it needs to be forcibly converted. For example: cstring STR; const char * P = (lpctstr) STR; // assume there is such a function, test (const char * P ); you can call test (STR) in this way; // here it will be implicitly converted to maid (2) getbuffer (INT nminbuflength). It is similar, it will return a pointer, but it is a little different, the returned value is lptstr (3). The two are essentially different. Generally, after the conversion, the return value is only used as a constant or an input parameter of the function. getbuffer (...) after the pointer is taken out, you can use this pointer to modify the content or make an output parameter of the function. (4) but here is another note: Str. after getbuffer (20), the allocation length of STR is 20, that is, the pointer P points to the buffer only 20 bytes long. When assigning a value to it, it cannot exceed, otherwise, the disaster is not far from you. If the specified length is smaller than the original string length, for example, getbuffer (1), it will actually allocate four bytes (that is, the original string length). In addition, when getbuffer (...) is called (...) you must call releasebuffer () to update the header information of the referenced memory block based on the string content. 3. Release of Copy & assign value & reference memory blockAlthough cstring can point multiple objects to the same referenced block storage, it is intelligent and safe to process various copying, assigning values, and changing the content of strings, it does not interfere with each other and does not affect each other. Of course, you must make sure that your code is correctly and appropriately used, especially when it is more complex to use, such as function parameters, references, and sometimes stored in the cstringlist, if a small part is improperly used, the result may lead to unpredictable errors. example: void test () {cstring STR ("ABCD"); // STR points to a referenced memory block (the reference count of the referenced memory block is 1 and the length is 4, the allocation length is 4) cstring A; // A points to an initial data status, A = STR; // A and STR point to the same referenced memory block (the reference count of the referenced memory block is 2, the length is 4, and the allocation length is 4) cstring B (); // A, B, and STR point to the same referenced memory block (the reference count of the referenced memory block is 3, the length is 4, and the allocation length is 4) {lpctstr temp = (lpctstr); // temp points to the first address of the string that references the memory block. (The reference count of the referenced memory block is 3, the length is 4, and the allocation length is 4) cstring d =; // A, B, D, and STR point to the same referenced memory block (the reference count of the referenced memory block is 4, the length is 4, and the allocation length is 4) B = "Testa "; // This statement actually calls the cstring: Operator = (cstring &) function. B points to the newly allocated referenced memory block. (The reference count of the newly allocated referenced memory block is 1, the length is 5, and the allocation length is 5) // At the same time, the reference count of the original referenced memory block is reduced by 1. a, D, and STR still point to the original referenced memory block (the reference count of the referenced memory block is 3, the length is 4, and the allocation length is 4)} // due to the end of D, call the destructor to reduce the reference count by 1 (the reference count of the referenced memory block is 2, the length is 4, and the allocation length is 4) lptstr temp =. getbuffer (10); // This statement will also cause a new memory block to be allocated. Temp points to the first address of the newly allocated referenced memory block (the reference count of the newly allocated referenced memory block is 1, the length is 0, and the allocation length is 10) // At the same time, the reference count of the original referenced memory block is reduced by 1. only STR still points to the original referenced memory block (the reference count of the referenced memory block is 1, the length is 4, and the allocation length is 4) strcpy (temp, "Temp "); // The reference count of the referenced memory block pointed by a is 1, the length is 0, and the allocation length is 10. releasebuffer (); // Note: The reference count of the referenced memory block pointed by a is 1, the length is 4, and the allocation length is 10} // run here, the lifecycle of all local variables has ended. The str a B object calls its own destructor // function, and the referenced memory block to which the object is directed is also reduced by 1. 4. Important FunctionsFreeextra () releases the allocated excess memory. Format (...) and formatv (...) actually use sprintf. Note that the special feature of the parameter is that the compiler cannot verify the type and length of the format string parameter and the corresponding variable element during compilation. Therefore, you must note that the two must correspond to each other; otherwise, errors may occur. The allocsysstring () and setsysstring (BSTR *) functions provide conversion between strings and BSTR. Note: When calling allocsysstring (), you must call it sysfreestring (...).

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.