说我创建一个成员变量指针pBuffer
。我将这个缓冲区发送到一些未知的地方,以填充数据。现在说pBuffer中有任意数量的数据。如何“重置”缓冲区?
问:有没有办法来重置p缓冲器而没有完全将其删除,同时还重新分配它占领的所有不必要的内存?
例子:
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
}
如果它只是一个缓冲区指针,“重新分配所有不必要的记忆”是删除它几乎100%。通常,开发人员不会释放内存,只需回收缓冲区并用新数据覆盖它(请参阅@andre)。 – 2013-02-12 21:17:56
等待,你想释放但不删除它?这完全没有意义! – 2013-02-12 21:18:05
..或换一种说法,请参阅@MooingDuck评论。 – 2013-02-12 21:19:06