Search code examples
c++bufferheap-memorycircular-bufferstack-memory

understanding memory allocation nature for a circular buffer implementation


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 ?


Solution

  • 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);