CString的局部变量使用GetBuffer
是否一定要使用ReleaseBuffer呢?
是否一定要使用ReleaseBuffer呢?
解决方案 »
- def文件报错。。
- 打算以后从事游戏开发,有什么图书介绍
- IObjectWithSite如何从SetSite中取得当前鼠标点击的URL?
- 用CRichEditView怎样才能让关键字显示不同颜色
- 很菜的问题,关于ASSERTHE 和CAutoLock的问题,请指教。
- 请问:用DAO是否只能打开ACCESS 97 不能打开2000 ?有没有方法打开2000的数据库呢?谢谢!
- 如何侦测另一个程序是否正在运行!
- 打印分页问提
- MFC的LIST Control显示图标问题
- 把从数据库中读出来的大字段内容传给客户端的问题
- 用TextOut往DirectX的surface输出文字之前能否描边?
- 关于OpenFileMapping
使用由 GetBuffer 返回的指针来直接将字符写入 CString 对象中。
调用 CString 对象的 ReleaseBuffer 来更新所有的内部 CString 状态信息,如字符串的长度。直接修改完 CString 对象的内容之后,在调用任何其他 CString 成员函数之前必须调用 ReleaseBuffer。
当时 字符串怎么都该不了了
releasebuffer之后就好了
GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象在以后的代码中继续可以实现长度自适应增长的功能。是否需要在GetBufer后面调用ReleaseBuffer(),是根据你的后面的程序是否需要继续使用该字符串变量,并且是否动态改变其长度而定的。不是什么好地编程习惯,之类的原因。
如果你GetBuffer以后程序自函数就退出,局部变量都不存在了,调用不掉用ReleaseBuffer没什么意义了。但如果你出现例如下面的代码
CString m_strA;
m_strA=_T("123456");
m_strA.GetBuffer(6);
m_strA+=_T("789");//此时就会出错了,因为需要动态加长字符串长度
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.The buffer memory will be freed automatically when the CString object is destroyed. Note that 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 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length.