MFC's CString Learning Notes-2

zhaozj2021-02-16  30

CSTRING CLASS Research (2)

3. CString Data Buffer

CString Use CStringData Structure as a buffer to store data and other information.

Struct CstringData

{

Long nrefs; // Reference Count

Int NDATALENGTH; // Length of data (including terminator)

INT Nalloclength; // Length of Allocation

// tchar data [nalloclength]

Tchar * data () // tchar * to managed Data

{Return (Tchar *) (THIS 1);

}

A cstring data buffer (cstringdata)

NREFS

NDATALENGTH

Nalloclength

Data ()

Data ....

CStringData :: DATA ()

CString :: M_PCHDATA

CString :: getData ()

CString :: Operator LPCTSTR ()

CStringData :: Data () Will Return to Actually Data Address.

CString :: M_PCHData Member Variable is a Pointer of Actually Data Address.

CString :: getData () Return CstringData, Point To The Buffer, Include Header And Actually Data.

_AFX_INLINE CSTRINGDATA * CSTRING :: getData () const

{Assert (m_pchdata! = Null); return (cstringData *) m_pchdata) -1;}

CString :: Operator LPCTSTR () Return The Address of M_PCHDATA.

_AFX_INLINE CSTRING :: Operator LPCTSTR () Const

{Return M_PCHDATA;

CString :: getBuffer () Function Will Do Following Process:

Create a new buffer;

Copy Old Value to New Buffer;

Dispose Old Buffer;

Return new buffer;

But if the CString have been referenced (the data buffer is not point to _afxPchNil), and the allocate length is bigger than required, It need not allocate new buffer, It will return current buffer only.

For example:

CString str1;

CHAR * BUF;

BUF = str1.getBuffer (10);

Before getBuffer () Function is invoked, The Address of str1 is 0x0012f6f8, The address of str1.m_pchdata is 0x5f4c4b14 (Should Be System Defined "Null" buffer, _afxpchnil).

And after GetBuffer () function is invoked, the address of str1 is 0x0012f6f8, the address of str1.m_pchData is 0x004213fc, Changed! .So that, the LPCTSTR () operator and GetBuffer () is different, LPCTSTR () operator will not create New buffer but return the capital buffer.

AND Other Example:

CSTRING STR1 ("This Buffer is Big Enough;

CHAR * BUF;

BUF = str1.getBuffer (10);

In this Example, The Address of Data Buffer is not change.

The Following Diagram Is Showing The Format of System Defined "Null" Buffer and Value:

NREFS = -1

NDATALENGTH = 0

Nalloclength = 0

Data ()

'/ 0'

_AFXPchnil

When the CSTING CREATE, The Default Data Buffer (M_PCHDATA) Always Point Here.

(CStringData *) _AfxDatanil

A CSTRING DATA BUFFER OF SYSTEM DEFAULT EMPTY

CString :: ReleaseBuffer () Function Will Update The "Header" Filed of CstringData.

Void CString :: ReleaseBuffer (int nnewlength)

{

CopyBeforeWrite (); // Just In Case GetBuffer Was Not Called

IF (nnewlength == -1)

nnewlength = lstrlen (m_pchdata); // Zero Terminated

ASSERT (NNEWLENGTH <= getData () -> nalloclength;

GetData () -> nDaTales;

m_pchdata [nnewlength] = '/ 0';

}

When client use GetBuffer () to get the value of CString, and change it, the header field is not changed, so if GetBuffer is called and data is modified, ReleaseBuffer () must be invoked to update the member varible of CStringData.

转载请注明原文地址:https://www.9cbs.com/read-27793.html

New Post(0)