如果去掉str.ReleaseBuffer();这行,就会显示234567811aaa 这是因为ReleaseBuffer()并不是释放整个Buffer,而是释放掉字符串后面无效的Buffer。 如果不执行,CString还是原来的长度,因为程序不清楚,分配的100这个大空间到底要用多少。 str += "aaa";这行为什么不自动计算长度,因为CString可以包含\0的, 比如用TCHAR *p =...
(因CString的内容改变了,而其长度未作相应修改,ReleaseBuffer完成此工作) 若buffer中string包含了结束符,则可用ReleaseBuffer(-1);若无结束符,则必须用ReleaseBuffer( nNewLength )添加结束符,指定其长度! CString对象销毁后,此buffer自动释放。
在调用ReleaseBuffer之后,由GetBuffer返回的地址也许就无效了,因为其它的CString操作可能会导致CString缓冲区被重新分配。 如果你没有改变此CString的 MFC string char cstring 类型转换 ,z_cCurTime,z_len,NULL,NULL ); 1 前言 今天在网上看论坛,发现大家对CString与Char *互转各说一词,其实我发现提问者所说的情况...
如果你使用由GetBuffer 返回的指针来改变字符串的内容,你必须在使用其它的CString 成员函数之前调用ReleaseBuffer 函数。 在调用ReleaseBuffer 之后,由GetBuffer 返回的地址也许就无效了,因为其它的CString 操作可能会导致CString 缓冲区被重新分配。如果你没有改变此CString 的长度,则缓冲区不会被重新分配。当此CString 对象...
CString之GetBuffer、ReleaseBufferLPTSTR GetBuffer( int nMinBufLength ); nMinBufLength为buffer的字符长度,不包括结束符 返回CString对象的内部buffer指针,非const,可以修改CString内容 void ReleaseBuffer( int nNewLength = -1 ); nNewLength为新的字符串长度,-1表示维持当前字符串长度 释放多余内存 注意点: ...
CString::ReleaseBuffer voidReleaseBuffer(intnNewLength**=-1);** Parameters nNewLength The new length of the string in characters, not counting a null terminator. If the string is null-terminated, the -1 default value sets theCStringsize to the current length of the string....
voidReleaseBuffer(intnNewLength**=-1);** Parameters nNewLength The new length of the string in characters, not counting a null terminator. If the string is null-terminated, the -1 default value sets theCStringsize to the current length of the string. ...
str.ReleaseBuffer(); //千万不能缺少 fclose(fp); } 上面的函数是GetBuffer函数最典型的用法了,其实它就相当于申请一块nLen大小的内存,只不过,这块内存是被引用在CString对象的内部而已,这是非常有效的一种用法,如果不直接用GetBuffer函数来申请的话,那么你必须用new操作符(或者malloc()函数)在CString的外部申请,...
CString::GetBuffer/ReleaseBuffer 技术内幕 CString采用copy-on-write机制(可以实现多个对象共享一块内存),高效而节省内存。 CString只有一个成员变量,长度为4,即 sizeof(CString) == 4: class CString { // functions ... protected: LPTSTR m_pchData; // pointer to ref counted string data...
能不能在ReleaseBuffer()之前通过字符串赋值放大字符串缓冲,我是以vc2010重新测试得出的结论,在其他开发环境未测试。 不过还是认为,ReleaseBuffer()之前直接操作CString字符串对象虽然没有错误,但不合适,因为字符串已经通过GetBuffer()把控制权已经给出去,再去干涉内部缓冲区,明显忘记了调用GetBuffer()的目的和意义。