title: Implementing a simple ring buffer for float values
body: <p>I am trying to implement a very simple ring buffer, for holding a stream of audio samples in the form of float values. </p>
  
  <p>I want to be able to take a snapshot of the audio input at any one time. I don't need to pop or delete any values, just keep a moving buffer of the last <em>n</em> samples. </p>
  
  <p>I'd like to ask if there are any potential issues with this implementation for my purposes. </p>
  
  <pre><code>class RingBuffer
  {
  public:
      RingBuffer (int bufferSize) : bufferSize (bufferSize), count (0), head (0)
      {
          buffer     = static_cast&lt;float *&gt; (malloc(bufferSize * sizeof(float)));
          readBuffer = static_cast&lt;float *&gt; (malloc(bufferSize * sizeof(float)));
      }
  
      ~RingBuffer ()
      {
          if (buffer != nullptr) free(buffer);
          buffer = nullptr;
  
          if (readBuffer != nullptr) free(readBuffer);
          readBuffer = nullptr;
      }
  
      void push (float value)
      {
  
          if (count &lt; bufferSize &amp;&amp; head == 0)
          {
              buffer[count++] = value;
          }
          else if (count == bufferSize)
          {
              // reset head to beginning if reached the end
              if (head &gt;= bufferSize)
              {
                  head = 0;
                  buffer[head] = value;
              }
              else
              {
                  buffer[head++] = value;
              }
          }
      }
  
      /**
       * Return a snapshot of the buffer as a continous array
       */
      const float* getSnapshot () 
      {
           // Set up read buffer as continuous stream
           int writeIndex = 0;
           for (int i = head; i &lt; count; ++i) 
           {
              readBuffer[writeIndex++] = buffer[i];   
           }
           for (int i = 0; i &lt; head; ++i)
           {
               readBuffer[writeIndex++] = buffer[i];
           }
           return readBuffer;
      }
  
  private:
      int bufferSize, head, count;
      float* buffer;
      float* readBuffer;
  };
  </code></pre>