观察任务管理器 getbuffer时有申请了内存,但是ReleaseBuffer时没有释放掉
#include "stdafx.h"
#include "afx.h"
int main(int argc, char* argv[])
{
CString tmp = "test\n";
for(int i=0;i<100;i++)
{
printf(tmp.GetBuffer(0));
tmp.ReleaseBuffer();
}
return 0;
}
#include "stdafx.h"
#include "afx.h"
int main(int argc, char* argv[])
{
CString tmp = "test\n";
for(int i=0;i<100;i++)
{
printf(tmp.GetBuffer(0));
tmp.ReleaseBuffer();
}
return 0;
}
其实它就相当于申请一块nLen大小的内存,只不过,这块内存是被引用在 CString对象的内部而已,这是非常有效的一种用法,如果不直接用GetBuffer函数来申请的话,那么你必须用new操作符(或者 malloc()函数)在CString的外部申请,然后再将申请的内存拷贝到CString对象中,显然这是一个非常冗余的操作,会使你函数的效率大大下降。
说白了 ReleaseBuffer就是更新赋值之后的字符串的长度, 而实际空间没有根本的变化。
Use ReleaseBuffer to end use of a buffer allocated by GetBuffer....he address returned by GetBuffer is invalid after the call to ReleaseBuffer or any other CString operation.
不过我想知道什么会调用cstring的析构函数啊,局部变量是不是函数结束就会啊,那么我看任务管理器,内存没降下来