c++ - 我如何 "reset"一个缓冲区?

标签 c++ pointers memory buffer

假设我创建了一个成员变量指针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/

相关文章:

C++ 读取内存地址/指针和偏移量

c++ - 转发类友声明

c - C 中带有 pthreads 的两个矩阵的乘积

c++ - 删除对象并在同一内存位置再次创建

MySQL不释放内存

memory - 了解 F# 内存消耗

c++ - const 是否适用于传递给函数的所有参数?

由于虚拟析构函数/clang 的 C++ 链接器警告

c++ - 为什么在 C++ 中使用指针?

c - 将结构传递给 pthread_create,强制转换后未定义值?