0

One of my class variables is a 2D array. The size depends on the user input. The user may input size which may exceed his hardware limit. So I want to handle this properly. Is the following code correct?

        int counter;
        try
        {
            int size = 20000;//this is actually from user input
            array = new double*[size];
            for(counter = 0; counter < size; counter++)
                array[counter] = new double[size];
        }
        catch(std::bad_alloc)
        {
            try
            {
                for(int i = 0; i < counter; i++)
                    delete[] array([i]);

                delete[] array;
                array = NULL;

                //display limitation message
                size = 2;
                array = new double*[size];
                for(int i = 0; i < size; i++)
                    array[i] = new double[size];
            }
            //catch again & exit application
        }
Cool_Coder
  • 4,888
  • 16
  • 57
  • 99
  • 1
    Why not use `std::vector`? – Sam Oct 21 '13 at 03:50
  • Use `std::vector` for nonsense-free code. – n. m. could be an AI Oct 21 '13 at 03:50
  • A program is correct if it implements its specification. (And usually, there are some unspoken parts of a specification, like that the program doesn't invoke undefined behaviors or outright crash). What is your specification? Are you asking whether your strategy for dealing with the allocation errors is a good strategy (as implemented in your program), or are you asking whether the intent/strategy that can be deduced from your code is actually implemented properly by the code? – Kaz Oct 21 '13 at 04:06
  • The code is not correct because `counter` is not in scope in all the places where it is referenced. Unless there is some other `counter` elsewhere, it will not compile. – Kaz Oct 21 '13 at 04:12
  • @Kaz, sorry that was a typo... :( The counter variable is now in scope & there is only one counter variable. – Cool_Coder Oct 21 '13 at 04:57
  • @Kaz Yes I was asking whether the strategy for dealing with the allocation errors is a good strategy or not. – Cool_Coder Oct 21 '13 at 04:58

1 Answers1

1

Your best bet is:

std::vector<std::vector<double>>  array(size, std::vector<double>(size));

But if you must do it manually then:

void init_array(int size)
{
    int counter;
    try
    {
        array = new double*[size];

        // Don't shadow counter here.
        for(counter = 0; counter < size; counter++)
        {
            array[counter] = new double[size];
        }
    }
    catch(std::bad_alloc)
    {
        // delete in reverse order to mimic other containers.
        for(--counter; counter >= 0;--counter)
        {
            delete[] array[counter];
        }

        delete[] array;

        // retry the call with a smaller size.
        // A loop would also work. Depending on context.
        // Don't nest another try{} catch block. because your code will
        // just get convoluted.
        init_array(size/2);
    }
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Sorry that was a typo... The counter variable is the same everywhere, i.e. there is only one counter variable. – Cool_Coder Oct 21 '13 at 05:00
  • And I did not understand why you are deleteing in reverse order? – Cool_Coder Oct 21 '13 at 05:01
  • I am not using vector because my application is for scientific computing & the vector may not give the best performance compared to double array. Your opinion on this? – Cool_Coder Oct 21 '13 at 05:03
  • @Cool_Coder: I think you are under the misaprehension that vector is slower than an array. This is not correct. See http://stackoverflow.com/a/3664349/14065 – Martin York Oct 21 '13 at 06:29
  • @Cool_Coder: Delete in reverse order because when the compiler allocates objects. It will write to de-allocate in the reverse order of creation. For doubles it is not a big deal but it is a convention worth following for maintenance reasons. – Martin York Oct 21 '13 at 06:31