1
votes

Unfortunately the documentation I have is either (a) original product documentation without any errrata (MS VC++ 6.0 help files) or (b) later MSDN help that applies to later MFC versions.

In particular:

[Q1] Is the operator += known to be buggy in VC++6 MFC CString? This code from VC++6 had to be fixed before it would even compile in a modern MFC app:

    CString szTemp;
    unsigned char m_chReceive[MY_BUF_SIZE];

    // compiles and seems to run but may be buggy in VC++6, won't compile in modern MFC
szTemp += m_chReceive; 
    // the above won't compile in modern MFC versions, but this "&+cast" does:
szTemp.Append( (const char *)&m_chReceive[0]); 

[Q2] Is it safe and robust to return a CString as the result of a function, in this manner, or does this cause memory corruption?

 CString MyClass:MyMethod(void)
 {   CString Stuff;
     // set a value to Stuff here.
     return Stuff; // return a stack-allocated-CString 
 }

I have code that uses the above two things all over the place and which also seems to exhibit random runtime memory corruption. Those two things are red flags to me, am I right in suspecting that CString was intended by the authors of MFC in Visual C++ 6.0 to be nice simple thing that you could use like it was an int or a char type, and return it from a function and somehow copy constructors and memory management all just worked?

Obvious stuff: Yes of course I will get all my code off VC++ 6.0 when I can, but I first need to patch a production system which is crashing, and then I can begin the huge task of moving this legacy codebase forward.

2
Related but far-too-vague question, with necessarily vague answers here: stackoverflow.com/questions/1102279/…Warren P
Is m_chReceive always NULL terminated?Sheng Jiang 蒋晟
It is always null terminated.Warren P

2 Answers

1
votes

According to Documentation for VC6.0

CString objects can grow as a result of concatenation operations. CString objects follow “value semantics.”

0
votes

Microsoft Documentation seems to indicate that CString in purpose is similar to std::string in that it grows automatically when needed, and can be safely be passed around as arguements or return values of functions.