分享
 
 
 

MFC的 CString 学习笔记 -3

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

CString Class Research (3)

4. CStringData Allocate and dispose.

In prior section, we know the CStringData has two Parts, One is Header, Include 3 member variables and 1 member function, totally 12 bytes, and the other part is actual data.

Look the following code in MFC to Allocate and Dispose CStringData and its data buffer.

CStringData* pData;

pData = (CStringData*)

new BYTE[sizeof(CStringData) + (nLen+1)*sizeof(TCHAR)];

pData->nAllocLength = nLen;

pData->nRefs = 1;

pData->data()[nLen] = '\0';

pData->nDataLength = nLen;

m_pchData = pData->data();

MFC allocate memory as BYTE array, include CStringData and data buffer, and convert it to CStringData pointer, nLen is the length of string, and one more byte, store a ‘\0’.

In debug mode, when the nLen <= 64, system allocates 64 bytes, when the nLen <= 128, system allocates 128 bytes....

delete[] (BYTE*)pData;

To dispose buffer, because there are CStringData part (header) and data buffer part, MFC convert the CStringData* to BYTE array, then delete it.

5. CString operator =.

There are two methods to set one CString’s value. If the source string is not a CString object, then MFC will allocate a new buffer to destination CString object, and use memcpy function to copy the content of source string to destination CString.m_pchData.

AllocBeforeWrite(nSrcLen);

memcpy(m_pchData, lpszSrcData, nSrcLen*sizeof(TCHAR));

GetData()->nDataLength = nSrcLen;

m_pchData[nSrcLen] = '\0';

In addition, if the source string is CString Object, commonly, the destination CString.m_pchData point to source CString.m_pchData. That means, the destination CString.m_pchData and the source CSting.m_pchData will point to the same address, destination CString and the source CString has the same CStringData object.

When 2 objects share one CStringData, the nRef member variable of CStringData will be set to 2.

If (m_pchData != stringSrc.m_pchData)

{

if ((GetData()->nRefs < 0 && GetData() != _afxDataNil) ||

stringSrc.GetData()->nRefs < 0)

{

// actual copy necessary since one of the strings is locked

AssignCopy(stringSrc.GetData()->nDataLength, stringSrc.m_pchData);

}

else

{

// can just copy references around

Release();

ASSERT(stringSrc.GetData() != _afxDataNil);

m_pchData = stringSrc.m_pchData;

InterlockedIncrement(&GetData()->nRefs);

}

}

Two CString share one CStringData will save system memory resource, but there is a risk, that when data in CString object’s CStringData is changed, the other CString object will be changed at the same time, that may be is not we want.

There is a function in MFC named CopyBeforeWrite to deal with this problem:

void CString::CopyBeforeWrite()

{

if (GetData()->nRefs > 1)

{

CStringData* pData = GetData();

Release();

AllocBuffer(pData->nDataLength);

memcpy(m_pchData, pData->data(), (pData->nDataLength+1)*sizeof(TCHAR));

}

ASSERT(GetData()->nRefs <= 1);

}

When one CString’s value will be set, MFC will check if there is any other CString share the same CStringData object with it. If the reference bigger than 1, that means at least 2 CString object reference this CStringData object. The current, will be changed CString must create a new CStringData object to store the new value.

In the Release() function, if the reference bigger than 1, the buffer will not delete actually.

6. LockBuffer() and UnLockBuffer()

LockBuffer will set nRef member to -1, and UnlockBuffer will set nRef member to 1.

MSDN said: While in a locked state, the string is protected in two ways:

No other string can get a reference to the data in the locked string, even if that string is assigned to the locked string.

The locked string will never reference another string, even if that other string is copied to the locked string.

CString str1, str2;

str2 = "abcde";

str2.LockBuffer();

str1 = str2;

CString str3;

str3 = "kk";

str3.LockBuffer();

str3 = str1;

The str2’s buffer is locked, when set str1 equal to str2, a new buffer will created to store the str2’s value. str1 and str2 have different CStringData object.

If the str3’s buffer is not locked, the str3 will share CStringData object with str1, but in this code, str3’s CStringData object is locked, MFC will copy the value of str1 to str3, the str3’s buffer address is not changed.

Of course , when CString is initializion without init-value, MFC will set a system defined, empty CStringData object to it, the nRef is -1, so LockBuffer() and UnLockBuffer() is no use to system defined, empty CStringData object.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有