到底干啥用的。有的说是:“这个函数会根据串内容来更新引用内存块的头部信息。”有的说“将队列返回给str”

解决方案 »

  1.   

    我平时用这个是将CSTRING换成CHAR *类型时使用的~CString p1 = "i love you";
    char *p = NULL;
    p = p1.GetBuffer(10);
    p1.ReleaseBuffer();
    AfxMessageBox(p);MSDN:
    Use ReleaseBuffer to end use of a buffer allocated by GetBuffer. If you know that the string in the buffer is null-terminated, you can omit the nNewLength argument. If your string is not null-terminated, then use nNewLength to specify its length. The address returned by GetBuffer is invalid after the call to ReleaseBuffer or any other CString operation.ExampleThe following example demonstrates the use of CString::ReleaseBuffer.// example for CString::ReleaseBuffer
    CString s;
    s = "abc";
    LPTSTR p = s.GetBuffer( 1024 );
    strcpy(p, "abc");   // use the buffer directly
    ASSERT( s.GetLength() == 3 ); // String length = 3
    s.ReleaseBuffer();  // Surplus memory released, p is now invalid.
    ASSERT( s.GetLength() == 3 ); // Length still 3
      

  2.   

    是啊,资料是说ReleaseBuffer()是释放内存。可是我测试如下:
    CString s;
    s = "abc";
    LPTSTR p = s.GetBuffer( 1024 );
    strcpy(p, "abc");   // use the buffer directly
    ASSERT( s.GetLength() == 3 ); // String length = 3
    s.ReleaseBuffer();  // Surplus memory released, p is now invalid.此时p与s的内存地址仍是一样,也可以引用的。
      

  3.   

    这个地址当然是一样的了,不过是如果在GetBuffer后不Release的话,
    Cstring中专门定义了一个结构体来描述这些信息,他的数据结构体中的长度这些信息将没有被重新初始化,这样数据就不对了,也就是说你在重新对s进行使用的时候,就会出现错误!struct CStringData{   long nRefs;             // reference count   int nDataLength;        // length of data (including terminator)   int nAllocLength;       // length of allocation   // TCHAR data[nAllocLength]    TCHAR* data()           // TCHAR* to managed data               { return (TCHAR*)(this+1); }};CString s;
    s = "abc";
    LPTSTR p = s.GetBuffer( 1024 );
    strcpy(p, "abc");   // use the buffer directly
    ASSERT( s.GetLength() == 3 ); // String length = 3
    //s.ReleaseBuffer();  // Surplus memory released, p is now invalid.
    strcpy(p,"a");
    ASSERT(s.GetLegth()==1);s的长度时不等于1的,他还等于3,它的结构体中字符长度的信息没有被更新!
      

  4.   

    另外:
    CString str,str1;
    str="abcd";
    str1=str;
    LPTSTR p,p1;       
    p1=p=str.GetBuffer(5);//此时p,p1都指向str的内存
    strcpy(p,"1");         //p,p1,str的内容都是“1” p1="aaa";              //只有p1的内容是“aaa”.  p和str都不变。这和上面的strcpy(p,"1")区别在什么地方呢?是不是p1指向的地址已经不一样了(我测试的结果也是如此,地址变了)
      

  5.   

    LPTSTR char* 他们定义的指针在给他们赋值的时候,系统会自己给他分配地址
    strcpy(p,"1");只是将1拷贝导p所指向的空间地址上。这是p必须是有效地址而p="aaaa";系统可以自动分配。
      

  6.   

    void CString::ReleaseBuffer(int nNewLength)
    {
    CopyBeforeWrite();  // just in case GetBuffer was not called if (nNewLength == -1)
    nNewLength = lstrlen(m_pchData); // zero terminated ASSERT(nNewLength <= GetData()->nAllocLength);
    GetData()->nDataLength = nNewLength;
    m_pchData[nNewLength] = '\0';
    }你看看这段代码就知道ReleaseBuffer有没有释放内存空间了
      

  7.   

    CString p1;
    char *p;
    p = p1.GetBuffer(10);
    p1.ReleaseBuffer();是和GetBuffer配对使用的,如果使用了GetBuffer后没有使用ReleaseBuffer那么就不能运行CString的其他函数,就是说再调用了GetBuffer后如果你要使用CString的其他函数你必须要调用RealeaseBuffer();