CString m;
m="ffff";
char* buf = new char[50];
memset(buf,0,50);
buf = m.GetBuffer(m.GetLength());
delete buf;这段程序在delete buf 时出错?请问是什么原因,如何解决。
多谢!!

解决方案 »

  1.   

    buff的原地址丢了,你把 CString 的地址给了buff, 
    delete buf 实际上是删除了CString储存字符的地方,当然会出错
      

  2.   

    buf = m.GetBuffer(m.GetLength());这一句后buf就指向了CString类 m 的缓冲区而不再是 你new 的空间!
    所以,delete buf必然出错
      

  3.   

    如果还想 删除 buf,应该怎样处理?
      

  4.   

    CString m;
    m="ffff";
    char* buf = new char[50];char *p;
    p=buf;

    memset(buf,0,50);
    buf = m.GetBuffer(m.GetLength());....
    delete []p;
      

  5.   

    去掉这一行:
    char* buf = new char[50];
    不必delete
      

  6.   

    char* buf=m.GetBuffer(m.GetLength());
    然后new和delete 都不用。或者
    char* buf = new char[50];
    memset(buf,0,50);
    strncpy(buf,m.GetBuffer(m.GetLength()),m.GetLength();
    m.ReleaseBuffer();
    delete buf;
      

  7.   

    CString m;
    int i ;
    m="ffff";char* buf = new char[50]; char* buf1 = buf ;
    for ( i=0; i<m.getlength(); ++i)
    {
        *buf1++ = m[i] ;  
    }delete buf;