Getbuffer releasebuffer cstring

Source: Internet
Author: User
Tags strtok

Getbuffer is used to save the pointer of the string buffer in the cstring class.
As for releasebuffer, there is such a sentence in msdn.
If you use the pointer returned by getbuffer
Change the string contents, you must call releasebuffer
Before using any other cstring member functions.
Call releasebuffer after using the pointer returned by getbuffer to use operations of other cstrings. Otherwise, an error occurs.

First, let's give an example. Cstring S ("ABCD ");
Int Len = S. getlength ();
Lptstr P = S. getbuffer (5 );
Strcpy (P, "hello ");

This is the first usage of getbuffer and the simplest one. You do not need to pass a parameter to it. It uses the default value 0, which means: "Give me the pointer to this string, I promise not to lengthen it ". When you call releasebuffer, the actual length of the string is recalculated and then stored in the cstring object.

If you need to modify the content in cstring, there is a special method that can be used, that is, getbuffer, which is used to return a writable buffer pointer.

If you only want to read the content in cstring, you only need to use getbuffer (0. If there are other operations next to cstring, immediately releasebuffer.

Others:
Getbuffer () will create the space of the specified size. This space can be modified.
In many cases, some APIs require a (char *) indicator as an output.
If we generate a (char *) buffer in this way, wait until the data is obtained.
You cannot use the convenience functions of cstring.
Therefore, it is better to use getbuffer () to generate a buffer space for him.
After the result is retrieved, we can directly use cstring to operate on it.
It is best to call releasebuffer () after getbuffer () is used.

Although the younger brother's website has used a lot of getbuffer (), an example is provided.
Cfile file;
// File_name is the file name defined in advance.
If (file. Open (file_name, cfile: moderead ))
{
Cstring szcontent;
Int nfilelength = file. getlength ();
File. Read (szcontent. getbuffer (nfilelength), nfilelength );
Szcontent. releasebuffer ();
// Put the obtained File Content in szcontent and we can operate on it.
}

Getbuffer/releasebuffer is a popular online saying: If you want to directly modify the internal data of cstring, you need to call getbuffer/releasebuffer. I agree with this statement.

The following are examples of errors to help you better understand them.
1

Cstring strtest
=
 
"
123
"

;

Char
*
P
=
Strtest. getbuffer (
0

);

Int
I
=

Atoi (P );

Strtest. releasebuffer ();

This method is correct, but I thinkGetbuffer/releasebuffer is unnecessary.
Why?Because
Int _ cdecl atoi (const char *)
The parameter is const char *, and the internal data of the cstring will not be modified.
.
So the aboveCodeCan be directly written

Cstring strtest
=
 
"
123
"

;

Int
I
=
Atoi (lpctstr) strtest );

Shun
Let's take a look at the getbuffer parameter problem. Many examples on the Internet are getbuffer (5)
A constant such as getbuffer (10 ).ProgramIt's impossible to know it in advance.
Strtest. getbuffer (strtest. getlength ()
In fact, getbuffer (0) can be used. The source code of getbuffer can be verified.

2

Cstring strtest

=
 
"
123 45
"

;



//
Some other code




Cstring strtest2
=

Strtest ;//The content of the next two cstrings is still stored in the same memory location.



Char
SEPs []
=
 
"
 
"

;


Char
*
Ptoken
=
 
0

;


//
Char * pstr = strtest2.getbuffer (0 );




Ptoken
=
Strtok ((
Char
*

) (Lpctstr) strtest2, SEPs );
// Ptoken
=
Strtok (pstr

, SEPs );




While

(Ptoken)

Ptoken
=

Strtok (null, SEPs );


// Strtest2.releasebuffer (0 );

the cstring class contains a special struct to record this information
struct cstringdata
{< br> long nrefs; // reference count
int ndatalength; // length of data (including Terminator) Data Length
int nalloclength; // length of allocation memory allocation length
// tchar data [nalloclength]
tchar * Data () // tchar * to managed data
{return (tchar *) (This + 1) ;}< BR> };
when assigning values, you simply add the reference count to 1, nrefs ++, then point to the same memory unit
when each object is destroyed, first reduce the reference count by 1 and nrefs --, and then determine whether it is 0, if it is 0, it is actually released

run the code above and we can see that the value of strtest has also changed. This is the origin of some strange problems related to cstring in the program. if the getbuffer/releasebuffer method in the comment is used, there is no problem at all.
same as
. For releasebuffer parameters, the default value is-1, but I do not recommend this parameter. because-1 indicates that the new length is determined by the current 00 Terminator position. in the above example
, strtok will reset the 00 Terminator. Therefore, the safe method is to set the length of the cstring to 0, releasebuffer (0 ), its content
has changed, and no one needs to use it.
note that the getbuffer/releasebuffer method can only change the strtest and strtest2. therefore, for a member variable, for example, m_strtest2 calling releasebuffer requires more attention, so there is no need to think so much about local variables.
how to get the most out of the box I realized that the program was wrong? ( char
*
) (lpctstr) is very dangerous. Remove const; otherwise, strtok cannot be compiled, and the importance of const is also illustrated from one aspect.

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.