0

I am trying to understand the memory allocation while using a third party written Ring Buffer implementation.

I referred to two freely available implementations of ring buffer :

  1. https://github.com/fbergama/MTCircularBuffer
  2. https://github.com/wizard97/Embedded_RingBuf_CPP

I tried to compile the available test programs in the above libraries on Ubuntu 16.04 PC. The libraries are provided with test programs. The test programs compile and I am able to use them successfully. However, what I want to know and understand is where does the memory for ring buffer elements ( in above libraries) get allocated ? Is it on the stack or the heap ?

tauseef_CuriousGuy
  • 770
  • 1
  • 12
  • 28
  • Do you know what [dynamic](https://stackoverflow.com/q/9181782/212858) allocation looks like in code? There is a keyword (or one of a few C functions) involved - did you search for it in the linked code? What did you find? – Useless Jan 30 '19 at 09:39

1 Answers1

0

where does the memory allocated for elements of ring buffer in above libraries get allocated ? Is it on the stack or the heap with the given test programs in the above repositories ?

if you do

{
   RingBufCPP<int, 10> rb1;
   RingBufCPP<int, 10> * rb2 = new RingBufCPP<int, 10>;
   ...
}

rb1 is on the stack and the value of rb2 on the heap

You have the choice because the size if known and there is no new to allocate the buffer in the implementation


But in MTCircularBuffer( size_t size ) there is a new, a part is allocated in the heap whatever you do MTCircularBuffer<int> rb1(10); or new MTCircularBuffer<int>(10);

bruno
  • 32,421
  • 7
  • 25
  • 37
  • Does this mean that with the given test program e.g., https://github.com/wizard97/Embedded_RingBuf_CPP/blob/master/examples_no_arduino/test.cpp, it is on the stack ? – tauseef_CuriousGuy Jan 30 '19 at 09:45
  • 1
    @tauseef_CuriousGuy yes it is, the buff is the attribute `Type _buf[MaxElements];` – bruno Jan 30 '19 at 09:47