Question

Is it good to construct CString from char[] ?

char R[5000];
CString s = R;

In these lines sometimes I have exception:

Windows has triggered a breakpoint in tst.exe.

This may be due to a corruption of the heap, which indicates a bug in tst.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while tst.exe has focus.

The output window may have more diagnostic information
Was it helpful?

Solution

The CString constructor attempts to copy a null-terminated string from the input argument (more precisely, from the memory address pointed by the input argument).

So it copies characters until reaching a 0 (null) character, and since the contents of R[5000] are not initialized, there's a good chance that none of the characters in it is equal to 0.

If there is no such character within the legal memory region pointed by the input argument, then the CString constructor exceeds that memory region, and most likely causes an illegal memory access.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top