1

I am getting run-error on providing default destructor. However, if left to compiler to provide default destructor it is running safely.

class foo
{
    private:
    int m_size;
    int* m_array;

    public:
    foo( int a_size ):m_size( a_size ), m_array( new int(a_size) ){}

    ~foo() {
        delete [] m_array;  
    }   
    void setArray();
};

void foo::setArray() 
{
    for( int i=0; i<m_size; ++i )
        m_array[i] = 10*i;
}

int main( int argc, const char* argv[] )
{
    class foo obj( 6 );
    obj.setArray();
    system( "pause" );
    return 0;
}

Runtime Error:

This may be due to a corruption of the heap, which indicates a bug in Destructors.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Templates.exe has focus.

The output window may have more diagnostic information.

Thanks.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • I don't know what language you're trying to make this look like, but your formatting drives me nuts. – Ben Voigt Dec 29 '10 at 01:18
  • @Ben Voigt Sorry! I am new to formatting on SO. What do you think is wrong ? – Mahesh Dec 29 '10 at 01:23
  • Spaces before semicolons comes to mind, also spaces around the scope operator `::`. They aren't illegal actually, but they are inconsistent with the code written by every other C++ programmer. The `for` loop definitely should be indented, since right now the close brace looks like it belongs to the for loop instead of the function. And I personally put opening braces of types and functions on a new line (but braces after `if`, `for`, `do`, and `while` on the same line), although that's not quite as universal. – Ben Voigt Dec 29 '10 at 01:28
  • @Ben Voigt Modified accordingly :) – Mahesh Dec 29 '10 at 01:35
  • That is SO much easier to read now. You can compare to my style shown in my answer, but what you have now is fine. BTW there are two common conventions for naming: one uses TitleCase for type names and camelCase for variables, while the other uses words_separated_by_underscores and appends _t to type names. – Ben Voigt Dec 29 '10 at 01:38

2 Answers2

4

new int(a_size) dynamically allocates a single int with initial value a_size.

I think you mean new int[a_size] which dynamically allocates an array of a_size ints.

(You should also provide a copy constructor and copy assignment operator for your foo class as the default ones will not do the correct thing. Better would be to replace your pointer member m_array with a std::vector<int> to manage the dynamically allocated memory automatically and then you wouldn't have to worry about a user-defined destructor, copy constructor and copy assignment operator.)

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
2

You allocated a single int, and then tried to delete an array.

The proper way to allocate an array of ints is:

new int[a_size]

Note the square (array) brackets, not parentheses.

As always, you should follow the rule of three and define a copy constructor as well.

EDIT: showing my preferred style:

class Foo
{
    int m_size;
    int* m_array;

public:
    Foo( int const a_size )
        : m_size(a_size)
        , m_array(new int[a_size])
    {}

    ~Foo( void )
    {
        delete [] m_array; 
    }

    void setArray(void);
};

void Foo::setArray( void )
{
    for( int i = 0; i < m_size; ++i )
        m_array[i] = 10*i;
}

int main( void )
{
    Foo obj(6);
    obj.setArray();
    system("pause");
    return 0;
}
Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • @Mahesh: It's actually FredOverflow you should thank, who put all that time into writing the comprehensive explanation. All I did was link to it. – Ben Voigt Dec 29 '10 at 01:24
  • I was simultaneously modifying my post too. Any how, Thanks and your code looks neat. – Mahesh Dec 29 '10 at 01:37