我想把char *Buf的值赋给CString Str,以下代码,为什么会出错?应该怎么修改,前提条件一定保留new char(50)分配内存
CString Str;
char *Buf;
Buf=new char(50);
strcpy(Buf,"ddd");
Str=Buf;
delete []Buf;

解决方案 »

  1.   

    那就试试 Str.Format("%s",Buf);
      

  2.   

    Str.Format("%s",Buf);
    这种方式也试过了,但是还是在delete []Buf;时候出错
      

  3.   

    CString str;
    char buf[50]; // or char *buf=new char[50];strcpy(buf, "ddd");
    str = buf;
    试试这样会不会出错
      

  4.   

    Buf=new char(50);
    ->Buf=new char[50];
      

  5.   

    CString Str;
    char *Buf;
    Buf=new char[50];  //()->[]
    strcpy(Buf,"ddd");
    Str=Buf;
    delete []Buf;
      

  6.   

    呵呵,确实可以了,看来我犯了一个低级的错误,把Buf=new char[50]写成Buf=new char(50);感谢以上各位的热心帮助,结贴!
      

  7.   

    The new operator attempts to dynamically allocate (at run time) one or more objects of type-name. The new operator cannot be used to allocate a function; however, it can be used to allocate a pointer to a function
    You programe modified by this:
    void CVC005Dlg::OnBnClickedOk()
    {
    CString Str;
    char* Buf ;

    Buf = _tcsdup(_T("ddd"));
    Str = Buf;
    free(Buf);
    AfxMessageBox(Str);
    }
      

  8.   

    楼主太不小心了,请把Buf=new char(50);这句话改成Buf=new char[50];再试试,还能有问题?