Hi All!
I am writing some Arduino code, and I really DO NOT want to use dynamic memory allocation (new/delete). The simpler I can keep the code the better (to avoid exceptions, unexpected errors, etc).
I need to have contained buffers of various sizes. I'm thinking of using class templates with a size parameter. But I also need a base class of the then instantiated temples to be able to pass the buffer around -
class BaseBuffer
{
private:
byte *m_pBuffer;
size_t m_sSize;
protected:
BaseBuffer(byte *pBuf, size_t sSize){ m_pBuffer = pBuf; m_sSize = sSize; }
....
<All routines to manipulate m_pBuffer>
....
};
template<size_t SZ>
class Buffer
{
static_assert(SZ > 0);
byte m_arBuffer[SZ];
Buffer() :
BaseBuffer(m_arBuffer, SZ)
{
}
};
Is there anything wrong with doing this?
I am a bit concerned about passing the
Buffer classes "m_arBuffer" variable address down to the base-class before the inheriting
Buffer class has finished its own construction. Though I've tried it and the compiler is not complaining and it seems to execute fine.
Any pointers (pun! ha!) on how this can be done better? I really want to avoid dynamic memory for a whole bunch of other reasons. I see this as an efficient approach, though something is nagging me that there may be a better way.
Any help?
What I have tried:
Still wracking my brain trying to see a better way of allocating various size static buffers inside a class, and having a common means of passing any template instance instantiations between common routines.