Repost an article from someone else first
These functions of the cstring class have been used for a long time, but they are not fully understood. From time to time, there are still errors. Today, I took the time to analyze them with Nico, which is to open the cloud:
Getbuffer and releasebuffer are a set of functions that need to be used in combination. Compared with getbuffersetlength, the advantage is that if the allocated space is greater than the actually saved string (ending with 0), releasebuffer will release the excess applied space, return to the system. However, pay attention to the following issues during use: If the string to be saved is ABC (ending with 0), the getbuffer parameter should be at least 3; if the content to be saved does not end with 0, for example, reading file data, if the getbuffer parameter is greater than the file length, the releasebuffer parameter must be the file length (if the getbuffer parameter is the file length, the releasebuffer parameter can be set to-1 by default )!
Cstring csstr;
Lptstr lpsz = csstr. getbuffer (100 );
Lpsz [0] = 'a ';
Lpsz [1] = 'B ';
Lpsz [2] = '/0 ';
Csstr. releasebuffer ();
Int nlength = csstr. getlength ();
/* The value of N is 2 */
Getbuffersetlength is relatively easy to understand. It applies for a specified length of space, even if the final stored string length is smaller than the applied length, it will not release the extra space.
Cstring csstr;
Lptstr lpsz = csstr. getbuffersetlength (100 );
Lpsz [0] = 'a ';
Lpsz [1] = 'B ';
Lpsz [2] = '/0 ';
Int nlength = csstr. getlength ();
/* The value of N is 100 */
For the red part, you did encounter this problem when writing your own code: the code is as follows:
Cstring temp;
Ulonglong dwcount = input_file.getlength ();
// Uint dwcount = (uint) input_file.getlength ();
Input_file.read (temp. getbuffer (dwcount), dwcount );
Temp. releasebuffer (dwcount );
If temp. releasebuffer () does not specify a parameter, this step will encounter an error. Therefore, for file read operations like this, it is better to specify a parameter the same as getbuffer when performing releasebuffer.
For
(If the getbuffer parameter is the file length, the releasebuffer parameter can be set to-1 by default )!
I set temp. releasebuffer (-1); the execution of this sentence still encountered an error, so it is better to specify the file length.
Next, let's look at other code.
Cstring STR;
Browseinfo Bi;
Tchar name [max_path];
Zeromemory (& BI, sizeof (browseinfo ));
Bi. hwndowner = getsafehwnd ();
Bi. pszdisplayname = Name;
Bi. lpsztitle = _ T ("select folder ");
Bi. ulflags = bif_returnfsancestors;
Lpitemidlist IDL = shbrowseforfolder (& BI );
If (IDL = NULL)
Return;
Shgetpathfromidlist (IDL, str. getbuffer (max_path ));
/// 1
// Cstring AA = Str. getbuffer (max_path );
// Cstring BB = STR; // after running this sentence, the STR content becomes garbled
// Int A = AA. getlength ();
// Int B = Str. getlength ();
// Lptstr cc = Str. getbuffer (max_path );
// Lptstr dd = BB. getbuffer (max_path );
// CC [1] = 'a ';
// BB. releasebuffer ();
Shows the debug parameters:
/// 2
Cstring AA = Str. getbuffer (max_path );
Int A = AA. getlength ();
Int B = Str. getlength (); // B cannot get the STR Length
Lptstr cc = Str. getbuffer (max_path );
CC [1] = 'a ';
Int e = Str. getlength (); // e cannot get the STR length correctly. The difference from 3 is that STR does not releasebuffer after the blue font getbuffer. // the proper method is to use STR twice. getbuffer and Str. both getlength () are under releasebuffer.
Str. releasebuffer ();
Int d = Str. getlength ();
The debug parameters are as follows:
/// 3
// Cstring AA = Str. getbuffer (max_path );
// Str. releasebuffer ();
// Cstring BB = STR; // The content of BB is correct.
// Int A = AA. getlength ();
// Int B = Str. getlength ();
// Lptstr cc = Str. getbuffer (max_path );
// Lptstr dd = BB. getbuffer (max_path );
// CC [1] = 'a ';
// Int d = Str. getlength (); // although the STR value can be obtained correctly, after getbuffer (), it is recommended that the releasebuffer () be performed once after CC [1] = 'A.
// Why is the releasebuffer before CC [1] = 'A' stopped? Although STR still becomes "ca /.....", However, according to msdn: after calling releasebuffer, the address returned by getbuffer may be invalid, because other cstring operations may cause the cstring buffer to be reallocated. If you do not change the length of the cstring, the buffer will not be reassigned. The proper practice is to release the buffer after CC [1 ].
The debug parameter values are as follows:
/// 4
Str. releasebuffer (); // release operation on the shgetpathfromidlist (IDL, str. getbuffer (max_path );
Cstring AA = Str. getbuffer (max_path );
Cstring BB = STR; // after this sentence is executed, the STR value will not become garbled, and it is similar to 1.
Int F = Str. getlength (); // compare with 1. The length can be obtained correctly here.
Str. releasebuffer ();
Int A = AA. getlength ();
Int B = Str. getlength ();
Cstring FF = Str. getbuffer (max_path );
Lptstr dd = BB. getbuffer (max_path );
Int d = Str. getlength ();
After a cstring is getbuffer, other cstring functions of the cstring are performed (especially "=" "+" and other cstring operations that are easy to ignore) previous releasebuffer (although the execution of several segments of code from this point of view, on the surface, after getbuffer is executed once, the cstring operation will not cause errors, but the second cstring operation will produce errors. For the sake of security, the function operation of cstring must be performed after getbuffer. releasebuffer () must be performed first ()).