Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 310
HoKy22 Avatar asked Oct 03 '22 20:10

HoKy22


1 Answers

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.

like image 192
abjuk Avatar answered Oct 07 '22 19:10

abjuk