Click here to Skip to main content
15,170,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 8-Feb-21 2:15am
Comments
Richard MacCutchan 7-Feb-21 4:33am
   
"The simpler I can keep the code the better"
So why have you made it so complicated? Using new/delete would be far simpler than what you have here.

I wouldn't state there is anything wrong with this but, in general, a template is not necessary for varying the size of an allocation. That can be just a parameter to a constructor or initialization method.

The actual fact is, what you are attempting will not compile as it is because you are not deriving your class from BaseBuffer.
   
@Rick York

How do you static allocate through a constructor? I.e how do you specify the length you want an enclosed array to be allocated to, so that you are not using the Heap memory area?

I was wondering if there was a way of specifying a member arrays length via the initialization list of a constructor. But I couldn't find anything. Seems strange that the language does not let you do this?!

I want to avoid new/delete to avoid issues with memory fragmentation, exceptions, out-of-memory errors at runtime, etc. There are a lot of good reasons to avoid it.

Thanks for spotting the typo. I should be inheriting from BaseBuffer.
   
Comments
Richard MacCutchan 8-Feb-21 5:05am
   
This is not a solution; please use the Have a Question or Comment? link below the above Solution to reply to Rick York.
Have you looked at using std::array?
See std::array - cppreference.com[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900