BUG: The CString::Delete() Function Does Not Return the Correct Value (250321)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q250321 SYMPTOMS
The CString::Delete function returns the length of the original string instead of the length of the modified string.
CAUSE
The CString::Delete function initializes a local variable (nNewLength) with the length of the original string and does not change this variable to reflect the length of the changed string.
Here is the source code for the Delete method of the CString class (reproduced from the Strex.cpp file, which is located in the Mfc/Src folder):
int CString::Delete(int nIndex, int nCount /* = 1 */)
{
if (nIndex < 0)
nIndex = 0;
int nNewLength = GetData()->nDataLength;
if (nCount > 0 && nIndex < nNewLength)
{
CopyBeforeWrite();
int nBytesToCopy = nNewLength - (nIndex + nCount) + 1;
memcpy(m_pchData + nIndex,
m_pchData + nIndex + nCount, nBytesToCopy * sizeof(TCHAR));
GetData()->nDataLength = nNewLength - nCount;
}
return nNewLength;
}
RESOLUTION
To work around this problem, call the GetLength method on the modified CString class to get its length:
CString str2 = "Hockey is best!";
str2.Delete(6, 3);
int n = str2.GetLength();
STATUSMicrosoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
Modification Type: | Major | Last Reviewed: | 12/11/2003 |
---|
Keywords: | kbBug kbpending kbString KB250321 |
---|
|