Wrapping a string buffer
===

We have a large C++ real-time program that uses buffers (e.g. `char str[500]`) of different sizes to store strings.

Due to some memory leakages we had in the past, we want to wrap the strings with a `MyString` class that would be initialized with the buffer and the size.

The problem is how to efficiently and easily allocate the buffers and the wrapper together, while keeping the buffer allocated as part of the class, and not allocated from the heap (see the next example).

I've tried using a template in the following manner:

    template <unsigned int N>
    class BufferString : public MyString
    {
    public:
       BufferString() : MyString(__buf, N) { }
       char __buf[N];
    };

So it could be allocated as class member or as an automatic variable:

    class SomeClass
    {
       BufferString<500> m_str;   // Need the buffer to be allocated in SomeClass
    };

    void foo()
    {
       BufferString<350> str;     // Need the buffer to be allocated on the stack
    }

However, the executable size grows dramatically using this solution, by about 1kB per instantiation of the template (probably due to the compilation of the constructor).

Is there a better way to do this?

Thanks.

