int nRetCode = 0;
     CString as;
 as="sdfsdfsdf";
// 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.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
}
   char*a=new char[as.GetLength()];
   strcpy(a,as);
   puts(a);
   delete[] a;  总是执行不料

解决方案 »

  1.   

    改为下面这两句

    char*a = new char[as.GetLength()+1]; 
    a[as.GetLength()] = '\0';
      

  2.   

    delete[] a; 执行出错 原因是 after normal block
      

  3.   

    建议改为:
    char *a=new char[as.GetLength()+1];
    strcpy(a,as);
    a[as.GetLength()]='\0';
    puts(a);
    delete[] a;
      

  4.   

    如果把 char *a=new char[as.GetLength()+1]; 
    strcpy(a,as); 
    a[as.GetLength()]='\0'; 
    puts(a); 
    delete[] a; 放在类中 就不行了 尤其是as中的字符一长 就出现上面的错误了
      

  5.   

    把a[as.GetLength()]='\0'; 放在 strcpy(a,as); 之前    strcpy是以'\0'作为结束符来操作的
      

  6.   

    都不行 我想了想char *a=new char[as.GetLength()+1]; 红色部分 设为常数 就行 目前只能这么样
      

  7.   

    用strncpy,另外你最好核实一下GetLength返回的是字符数而不是字节数,而strcpy是字符串的ascii版本,一个字符就是一个字节;CString会根据当前编码,一个字符可能是一个字节,也可能是多个字节。