TransWikia.com

Possible dynamic allocation issues

Stack Overflow Asked by grango on February 14, 2021

Right now i’m trying to get my code(simple POD containers) running as stable as humanly possible. My main concern is memory allocation and deallocation(new[] and delete[] operators). Is it possible to get any undesired behavior out of them(like SIGSEGV or exceptions)?
Here’s a little test example i wrote:

class my_vector{
    private:
        long* _data;
        size_t _size;
        size_t _capacity;
    public:
        my_vector()
       {
           this->_data = new long[10];
           this->_size = 0;
           this->_capacity = 10;
       };

        ~my_vector()
       {
           delete[] this->_data;
       };

        void add(long value)
       {
           if (this->_size == this->_capacity)
                 this->expand();
           this->_data[this->_size] = value;
           this->_size++;
       };
    private:
        void expand()
        {
           long* tmp = new long[this->_capacity*2];
           memcpy(tmp, this->_data, sizeof(long)*this->_size);
           this->_capacity *= 2;
           delete[] this->_data;
           this->_data = tmp;
        };
}

One Answer

A new[] expression, by default, throws an exception if it fails (e.g. if a dynamic memory allocation fails).

If the constructor of any of the objects being constructed fails by throwing an exception, then the new [] expression will have the (net) effect of throwing that exception. Some other things also happen before the exception is finally emitted - for example, if construction of the third object in a dynamically allocated array fails, the destructors for the two previously constructed objects will be called. As far as calling code is concerned, if an exception is emitted, then none of the objects created by the new[] expression have ever existed. This is specified in the standard.

If an exception is thrown and never caught, the program will terminate. That is normal and expected.

A delete [] expression will generally not throw unless a destructor of an object being destroyed throws. Defining a destructor which throws is usually considered very poor practice, so should be avoided.

There is a nothrow form of a new expression, that does not - in itself - throw exceptions. In that case, the new expression gives a null pointer on failure.

new[] and delete [] expressions do not, in themselves, cause SIGSEGV, or any other form of abnormal termination. If that occurs, it is a sign that some other code has exhibited undefined behaviour (e.g. written past the end of an array). That can be in the constructor or destructor of the objects in the array, or in other code.

Correct answer by Peter on February 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP