1

Say I create a member variable pointer pBuffer. I send this buffer into some unknown land to be filled with data. Now say pBuffer has an arbitrary amount of data in it.

Q: Is there a way to reset pBuffer without completely deleting it, while still deallocating all unnecessary memory it was occupying?

Example:

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
}
5
  • 2
    If it's just a buffer pointer, 'deallocating all unnecessary memory' is just about 100% of deleting it. Usually, developers don't deallocate the memory, just recycle the buffer and overwrite it with new data, (see @andre). Feb 12, 2013 at 21:17
  • 1
    Wait, you want to deallocate but not delete it? That makes no sense at all! Feb 12, 2013 at 21:18
  • ..or, to put it another way, see @MooingDuck comment. Feb 12, 2013 at 21:19
  • Well since I don't know how much data will be put into the buffer, the first time it could occupy 1,000,000 bytes, and the second time it could only occupy 1 byte. I would like to deallocate the 1,000,000 bytes and "start from scratch" in a way, so that I don't have all 1 million bytes allocated the second time if I only am going to need 1.
    – xcdemon05
    Feb 12, 2013 at 21:20
  • It's not worth it. Use a pool of reasonably-sized buffers and give up on new, delete, realloc, whatever during your run. If your server, (or whatever), frequently has to deal with different protocols with widely-varying payloads, use an array of buffer poos with different size buffers in each, (128, 1K, 4K, 16K etc). Always start with 128. If the 'whatever' returns with 128 bytes, use the next size up, if the buffer returns not filled, use the next size down, (or start with 128 again). Feb 13, 2013 at 8:13

3 Answers 3

1

Try realloc() if you know the amount of stuff in the buffer vs the remaining space in the buffer.

1

Using only a single raw pointer, no; but if you keep a size variable you can reset the buffer relatively easily.

However, this being tagged as C++, I would like to caution you from doing this and will instead propose an alternative. This meets your requirement of allowing memory to be allocated then later for the buffer to be "reset", without deallocating the memory. As a side benefit, using std::vector means that you don't have to worry about the memory leaking in subsequent calls to FillBuffer(), specifically when the existing buffer is too small and would need to be reallocated.

#include <vector>

class Blah
{
public:

    std::vector<unsigned char> pBuffer;

    Blah(){}
    ~Blah(){}

    FillBuffer()
    {
        //fill the buffer with data, doesn't matter how
    }

    ResetBuffer()
    {
        pBuffer.clear();

        // if you _really_ want the memory "pointed to" to be freed to the heap
        // use the std::vector<> swap idiom:

        // std::vector<unsigned char> empty_vec;
        // pBuffer.swap(empty_vec);
    }
};
1
  • 1
    First sentence is misleading, since it can easily be done with just raw pointers. A pointer to the beginning of the data, a pointer to the end of the data, and a pointer to the end of the buffer. Feb 12, 2013 at 21:20
0

Buffers typically need a maximum size and a current size. To "reset", you would set the current size to zero. When you use it again, you might need to grow or shrink the maximum size of the buffer. Use realloc or malloc/new and memcpy (which realloc does internally when growing) to move existing data to the new buffer.

Note that these are expensive operations. If you expect the buffer to need to grow from use to use, you might consider doubling its maximum size every time. This effectively amortizes the cost of the allocation and copy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.