来自char []的CString构造函数

时间:2014-05-06 08:29:29

标签: visual-c++

从char []构建CString是否合适?

char R[5000];
CString s = R;

在这些行中,有时我有例外:

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

1 个答案:

答案 0 :(得分:2)

CString构造函数尝试从输入参数复制以null结尾的字符串(更确切地说,从输入参数指向的内存地址)。

因此它会复制字符直到达到0(null)字符,并且由于R[5000]的内容未初始化,因此很可能其中的所有字符都不等于0。 / p>

如果输入参数指向的 legal 内存区域中没有此类字符,则CString构造函数超出该内存区域,并且很可能导致非法< / strong>内存访问。