Search code examples
c++msdngetbuffer

CString::GetBuffer() example


I am trying to understand the GetBuffer() function. Looks like it returns you the pointer to the CString, which is confirmed in msdn GetBuffer(). However, I don't understand the example shown in the msdn GetBuffer().

LPTSTR p = s.GetBuffer( 10 );

Is there a reason why it's 10 inside? Can anyone show me the output of the example?


Solution

  • The 10 is the minimum buffer length, so if you call GetBuffer() on a CString of, say, 4 characters it will allocate an LPTSTR 10 chars long, in case you want to strcpy a longer string into that buffer (as they do in the example). The 10 in the example is arbitrary, they could just as easily used 6 (five letters in "Hello" plus the terminating null) or any larger number and it would have worked the same.

    In general, though, you'll be better off steering clear of GetBuffer() unless you really need to use it.