Replacing legacy C-style buffers

2016/09/03

Tags: c++ binary std string c vector legacy buffer to stl void to std vector

Since I’ve been working lately on legacy projects, filled with C-style buffer containers like void*, char*, byte*, I’ve started replacing these types of buffers with something a bit more safe and modern, if I might say so.

The new buffer containers I’ve used are std::vector. The replacement would look like this:

    void* pBuffer = ...;  
    size_t size = ...;  

Of course, it’s not a 1-size-fits-all solution. There may be times when you have to use the raw, dynamic allocated buffers and pass them around, depending on the context.
I’ve seen people use std::string for the same purpose of holding binary data. It is possible, but it may be misleading to maintainers, since strings are supposed to be strings and contain string data.