假设我创建了一个成员变量指针pBuffer
。我将此缓冲区发送到某个未知区域以填充数据。现在假设 pBuffer 中有任意数量的数据。
问:有没有办法在不完全删除 pBuffer 的情况下重置它,同时仍然释放它占用的所有不必要的内存?
例子:
class Blah
{
public:
unsigned char* pBuffer;
Blah(){pBuffer = NULL;}
~Blah(){}
FillBuffer()
{
//fill the buffer with data, doesn't matter how
}
ResetBuffer()
{
//????? reset the buffer without deleting it, still deallocate memory ?????
}
};
int main()
{
Blah b;
b.FillBuffer();
b.ResetBuffer();
b.FillBuffer(); //if pBuffer were deleted, this wouldn't work
}
最佳答案
如果您知道缓冲区中的内容量与缓冲区中的剩余空间,请尝试 realloc()
。
关于c++ - 我如何 "reset"一个缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841927/