CString and GetLength

I ran into this problem recently when debugging some native code and thought that it will be good to share this with everyone.


CString sampleString = CString(_T(“Sample\0String”), 14);

int len = sampleString.GetLength();          // len is 14

CString trimmedString = sampleString.Trim(); // trimmedString = “Sample”

CString newstring = CString(sampleString);   // newString = “Sample”

len = newstring.GetLength();                 // len = 14


CString GetLength returns the length that you passed to it in the constructor and not the length of the string. This can be confusing if you copy the string and loop through the length. It can also be the cause of bugs is you get the length and use CString.GetBuffer() and loop through the buffer for the length.

It looks like CString.GetLength() is the size of the internal buffer and nothing more.


MSDN documentation for CString – http://msdn.microsoft.com/en-us/library/aa300471(VS.60).aspx

Comments (12)

  1. MSDN Archive says:

    Nice post. Where is CString.Trim() in MSDN? Is it typo of CString.TrimRight()?

  2. Thottam Sriram says:

    Interesting, I could’t find it either. But it compiles and works. It is supposed to remove leading and trailing white spaces I would think.

  3. Tremonk says:

    Good one !!! Bro, do you remember me ?

  4. Patrik Ehringer says:

    Am I correct to assume that this is a heavy bug?

    Just tried this code in Visual Studio 2005 and it really behaved as described.

    I was sure that CString::GetLength() would always return the number of charactes up to the first null terminator (without including it in the count). All my code relies on that assumption…

  5. Thottam Sriram says:

    I would agree. I did not realize this until I ran into this issue recently. The assumption that CString makes may be is that the data in the buffer is not binary and does not contain null terminators in it. But that assumption is not evident from the documentation.

  6. Patrik Ehringer says:

    Do you have any plans to officially report this bug to Microsoft?

    If not, do you know what would be the place to do so on my own? And also in this case, would I be allowed to copy/paste your code from above into this bug report?



  7. Thottam Sriram says:

    Let me try to contact the right owners and see what they have to say. In the mean time feel free to use the code above.

  8. NMGod says:

    use strlen()

    I’m pretty sure GetLength can only bu used with assert, like an if statment.

  9. Quin Arnold says:

    Use ReleaseBuffer for memory modifications that are external to the CString Class.

  10. Ilya Ilba says:

    This is not a bug. CString can contain null characters by design (it was mentioned somewhere in documentation). And Trim() doesn't affect this string, because there is no white space characters in the string (null character isn't a white space). The statement in the comment for line 3:

    [CString trimmedString = sampleString.Trim(); // trimmedString = "Sample"]

    is incorrect. You can see "Sample" for sampleString value in debugger window, but it's not true (debugger bug). In fact the string value is the same as sampleString, i.e. "SampleString".

    You can watch the sampleString in debugger, and you will see "Sample" value for it too (as I said, debugger bug, quite confusing).

  11. Ilya Ilba says:

    Here we are,


    "A CString object keeps character data in a CStringData object. CString accepts null-terminated C-style strings, but does not retain the null character in the stored character data. Instead, CString tracks string length. CString does provide a null terminator when it exports a C-style string. You can insert a null character amidst stored character data, but it may produce unexpected results."