Delete array of size 1

Posted by Arne on Stack Overflow See other posts from Stack Overflow or by Arne
Published on 2010-06-07T11:28:30Z Indexed on 2010/06/07 11:32 UTC
Read the original article Hit count: 490

Filed under:
|

This is possibly a candidate for a one-line answer. I would like know it anyway..

I am writing a simple circular buffer and for some reasons that are not important for the question I need to implement it using an array of doubles. In fact I have not investiated other ways to do it, but since an array is required anyway I have not spent much time on looking for alternatives.

template<typename T>
class CircularBuffer
{
public:
    CircularBuffer(unsigned int size);
    ~CircularBuffer();
    void Resize(unsigned int new_size);
    ...
private:
    T* buffer;
    unsigned int buffer_size;
};

Since I need to have the buffer dynamically sized the buffer_size is neither const nor a template parameter. Now the question:

During construction and in function Resize(int) I only require the size to be at least one, although a buffer of size one is effectively no longer a buffer. Of course using a simple double instead would be more appropriate but anyway.

Now when deleting the internal buffer in the destructor - or in function resize for that matter - I need to delete the allocated memory. Question is, how? First candidate is of course delete[] buffer; but then again, if I have allocated a buffer of size one, that is if the pointer was aquired with buffer = new T[0], is it still appropriate to call delete[] on the pointer or do I need to call delete buffer; (without brackets) ?

Thanks, Arne

© Stack Overflow or respective owner

Related posts about c++

Related posts about memory-management