GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象在以后的代码中继续可以实现长度自适应增长的功能。

CString ::GetBuffer有两个重载版本:

LPTSTR GetBuffer( );

LPTSTR GetBuffer(int nMinBufferLength);

在第二个版本中,当设定的长度小于等于原字符串长度时,该参数会被忽略,不分配内存,指向原CString;当设定的长度大于原字符串本身的长度时就要重新分配(reallocate)一块比较大的空间出来。而调用第一个版本时,应如通过传入0来调用第二个版本一样。

是否需要在GetBufer后面调用ReleaseBuffer(),是根据你的后面的程序是否需要继续使用该字符串变量,并且是否动态改变其长度而定的。如果你GetBuffer以后程序自函数就退出,局部变量都不存在了,调用不调用ReleaseBuffer没什么意义了。

GetBuffer(int size)是用来返回一个你所指定大小可写内存的成员方法。它和被重载的操作符LPCTSTR还是有点本质区别的,LPCTSTR是直接返回一个只读内存的指针,而GetBuffer则是返回一个可以供调用者写入的内存,并且,你可以给定大小。下面是个简单的,但也是非常典型的例子:
int readFile(CString& str, const CString& strPathName)
{
        FILE* fp = fopen(strPathName, "r"); // 打开文件
        fseek(fp, 0, SEEK_END);
        int nLen = ftell(fp); // 获得文件长度
        fseek(fp, 0, SEEK_SET); // 重置读指针
        char* psz = str.GetBuffer(nLen);
        fread(psz, sizeof(char), nLen, fp); //读文件内容
        str.ReleaseBuffer(); //赋值的时候千万不能缺少
        fclose(fp);
}
    上面的函数是GetBuffer函数最典型的用法了,其实它就相当于申请一块nLen大小的内存,只不过,这块内存是被引用在CString对象的内部而已,这是非常有效的一种用法,如果不直接用GetBuffer函数来申请的话,那么你必须用new操作符(或者malloc()函数)在CString的外部申请,然后再将申请的内存拷贝到CString对象中,显然这是一个非常冗余的操作,会使你函数的效率大大下降。
    ReleaseBuffer函数是用来告诉CString对象,你的GetBuffer所引用的内存已经使用完毕,现在必须对它进行封口,否则 CString将不会知道它现在所包含的字符串的长度,所以在使用完GetBuffer之后,必须立即调用ReleaseBuffer函数重置 CString的内部属性,其实也就是头部信息。

补充一下:

GetBuffer说白了就两个功能:

1:就是将CString里面的内存交到外部一个来处理,外部可以直接修改它的内容。

2:重新修改CString的内存大小,这个数值不包含null结尾符。

另一个典型的用法:就是将CString里面的内容变为int或long型,需要先获取里面的内存指针。这样就可以先GetBuffer(内存大小)方便直接转换。

如果在外部修改了CString里面的内容,在重新使用CString之前,需调用ReleaseBuffer()也就是说,ReleaseBuffer不需要每次都调用。

MSDN原文:

If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CSimpleStringT member methods.

The buffer memory is automatically freed when the CSimpleStringT object is destroyed.

If you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 (the default) for the length to ReleaseBuffer, and ReleaseBuffer will perform a strlen on the buffer to determine its length.

Call GetBuffer for a CString object and specify the length of the buffer you require. Use the pointer returned by GetBuffer to write characters directly into the CString object. Call ReleaseBuffer for the CString object to update all the internal CString state information, such as the length of the string. After modifying a CString object's contents directly, you must call ReleaseBuffer before calling any other CString member functions.

http://www.cnblogs.com/jamesmile/archive/2010/04/19/1715756.html

Cstring GetBuffer 和 ReleaseBuffer相关推荐

  1. CString:Getbuffer和Releasebuffer的作用

    首先看MSDN中的解释: CString::GetBuffer  LPTSTR GetBuffer( int nMinBufLength );    throw( CMemoryException ) ...

  2. CString的GetBuffer与ReleaseBuffer

    http://blog.pfan.cn/xman/43212.html http://www.cnblogs.com/jamesmile/archive/2010/04/19/1715756.html ...

  3. CString之GetBuffer、ReleaseBuffer

    LPTSTR GetBuffer( int nMinBufLength ); nMinBufLength为buffer的字符长度,不包括结束符 返回CString对象的内部buffer指针,非cons ...

  4. CString的GetBuffer和ReleaseBuffer

    GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象在以后的代码中继续可以实现长度自适应增长的功能. CString ::GetBu ...

  5. MFC学习 CString内存泄漏的雷区——GetBuffer与ReleaseBuffer

    GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象在以后的代码中继续可以实现长度自适应增长的功能. CString ::GetBu ...

  6. GetBuffer与ReleaseBuffer的用法,CString剖析

    转载: http://blog.pfan.cn/xman/43212.html GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象 ...

  7. CString之GetBuffer与ReleaseBuffer

    我们知道,CString是MFC中提供的方便字符串操作的一个类,非常好使,具有自动动态内存管理功能. GetBuffer()主要作用是将字符串的缓冲区长度锁定: ReleaseBuffer()则是解除 ...

  8. 实例测试CString的GetBuffer与ReleaseBuffer功能和注意点

    实例测试CString的GetBuffer与ReleaseBuffer功能和注意点 编者:李国帅 qq:9611153 微信lgs9611153 时间:2019-02-22 背景原因: 回顾来自200 ...

  9. MFC学习(27)CString内存泄漏的雷区——GetBuffer与ReleaseBuffer

    GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象在以后的代码中继续可以实现长度自适应增长的功能. CString ::GetBu ...

最新文章

  1. 【Unity】5.1 3D坐标系基础知识
  2. openstack的网络、子网、端口的关系
  3. kill -3 获取threaddump信息---转载
  4. 【转摘】如何得心应手的玩转Excel
  5. TODO-MVP-Loaders源码体验
  6. rollup学习小记
  7. linux 独占 cpu,宋宝华:谈一谈Linux让实时 高性能任务独占CPU的事
  8. CANOpen紧急报文
  9. ubuntu之解决挂载NTFS磁盘时出现input/output error
  10. python自学-Python 应该怎么学?
  11. @primary注解_springboot整合redis分别实现手动缓存和注解缓存
  12. C++中struct和class的区别 [zz]
  13. 大数据Hadoop生态圈
  14. excel服务器模板修改,勤哲Excel服务器设计查询模板
  15. 计算机二级用的ms什么版本,计算机二级ms office用的哪个版本
  16. word中有软回车(每行后面有向下的箭头)的解决方法
  17. sql实现查询两个时间之间每月的数量
  18. 计算机应用基础教研设想,计算机应用基础、信息技术与课堂教学深度融合,这所学校这样做...
  19. SAP-ABAP-小计收起/折叠明细项目-只显示小计内容
  20. 一杯清茶nbsp;几许相思

热门文章

  1. 个人博客模板 html5
  2. 京东搜索--es和springboot加前端vue
  3. 【从零开始的Java开发】2-9-3 油画商城静态网页案例
  4. Codethink开源是入职流程的一部分
  5. 调整 linux mint亮度,Linux Mint 亮度调节——xrandr命令学习
  6. Java字符串数组转集合方法
  7. 利用DB实现分布式锁的思路
  8. 特征码的使用办法_个人码 VS 商家收款码
  9. 企业微信服务端API的理解(开发指南部分)
  10. 师从英国两院院士|生物医学科研人员获CSC资助赴剑桥大学访学