堆内存数组操作问题?
我用Wizard创建了一个支持MFC的Win32 Console Application,程序代码如下:CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr < < _T( "Fatal Error: MFC initialization failed ") < < endl;
nRetCode = 1;
}
else
{
// TODO: code your application 's behavior here.
char* c=new char[6];
strcpy(c,(const char*) "111 ");
char* d = "sss ";
strcat(c,d);
printf( "the string is %s\n ", c);
delete []c; // 程序运行到这里crash,为什么?
c = NULL;
}
return nRetCode;
}
[解决办法]
char* c=new char[6];
===>
char* c=new char[7];
[解决办法]
char* c=new char[7];
空间不足
[解决办法]
strcat(c,d); //这里就操作非法内存空间
printf( "the string is %s\n ", c);
[解决办法]
没有结束标志