mfc - Is a CString always null-terminated? -


from cstring interface, 1 should not assume cstring null-terminated. however, seems there is, in fact, null character @ end of string. possible, in windows implementation, create cstring not have null character, reading 1 character past end of string looking @ different heap object?

a cstring more visual basic string or bstr. can contain embedded binary zeros in data portion of cstring. when using various operators convert between cstring , standard c type 0 terminated strings, embedded binary 0 treated end of string character. cstring lot bstr type of variable.

for instance put following source lines mfc project , ran in visual studio c++ debugger.

cstring mystring (_t("this\000is string."));  // mystring contain "this" 0 terminated string.  cstring myjj; myjj.format (_t("this%cisaxxx"), 0);   // creates string embedded binary 0 in it. int ilen = myjj.getlength();      // returns length of complete string, 11 characters cstring myright = myjj.right(4);  // returns right 4 characters, "axxx" tchar  mytbuff[64]; _tcscpy (mytbuff, myjj);    // copies string embedded binary 0 mytbuff 

as far going next heap object, not depend on that. implementation of cstring how object laid out in memory , how using memory. may if create cstring, buffer sized 64 characters allocated regardless of how few characters put it. cstring provides getlength() method find out how many characters in cstring should used. there methods getting , setting specific character positions.

cstring designed allow programmer think in terms of strings in visual basic types of strings rather having deal c style strings arrays of characters special end of string terminator character, binary zero.

edit01 - compiler arguments , effects on cstring

the visual studio compilers previous visual studio 2013 allowed cstring class create either 8 bit multi-byte character set or 16 bit unicode character strings depending on whether _mbcs or _unicode defined when processing source file.

the reason previous visual studio 2013 appears using _mbcs deprecated (see side-effect of deprecation of mbcs support mfc in vs 2013).

the root of flexibility tchar definition may either char if _mbcs defined or wchar_t if _unicode defined. in turn determines happens _t() or text() macro turn quoted string either array of type char or array of type wchar_t either pre-pending or not l used indicate wchar_t text string. influences actual type of lpctstr (pointer const tchar string) or lptstr (pointer non-const tchar string).

windows 95/98/me did not have native support unicode in windows api did windows nt/2000/xp allowing choice of unicode target windows nt or mbcs target windows 95 helpful. option @ time microsoft layer unicode provided unicode interface windows api windows 95/98/me.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -