0

I have a problem with two dimensional dynamically allocated array I'm using in my code. Everything works fine until my program tries to call the destructor of my tablica2D object. I get a runtime error "HEAP CORRUPTION DETECTED" when my program gets to the last delete[] tab command. Does this mean that the loop preceding it already deallocates all of memory assigned to tab? I was under the impression that to deallocate all of dynamically assigned memory there needs to be one delete command for each new command. Or is something else causing this error?

Here is the code of the class that's causing me trouble:

class tablica2D
{
    static const int k = 2;
    int n, m;
    string **tab;
public:
    tablica2D(int n, int m)
    {
        this->n = n;
        this->m = m;

        tab = new string*[n];
        for (int i = 0; i < m; i++)
        {
            tab[i] = new string[m];
        }
    }
    string* operator [](int n)
    {
        return tab[n];
    }
    static const bool compareRows(const string* i, const string* j)
    {
        int x = atoi(i[k].c_str());
        int y = atoi(j[k].c_str());
        return x > y;
    }
    void sort()
    {
        std::sort(tab, tab + n, compareRows);
    }
    ~tablica2D()
    {
        for (int i = 0; i < n; i++)
        {
            delete[] tab[i];
        }
        delete[] tab;
    }
};
6
  • 1
    Is there a reason you don't use std::vector? Commented Apr 5, 2015 at 16:07
  • 1
    Why for heaven' s sake you're struggling with memory management on your own, where it's all perfectly available with standard c++ containers and dynamic memory management? Commented Apr 5, 2015 at 16:09
  • @πάνταῥεῖ Perhaps it's an assignment. Commented Apr 5, 2015 at 16:11
  • @RawN There seems to be an epidemic of teachers assigning things like this, and never mentioning to the student all that's required to implement this properly. For example, issues such as the "rule of 3", which the OP failed to implement. Commented Apr 5, 2015 at 16:16
  • 1
    @PaulMcKenzie No doubt about that. I've seen my fair share of that nonsense and pseudoscience here in SE Europe. What they 'teach' here could be easily labeled as crime. Commented Apr 5, 2015 at 16:20

2 Answers 2

2

You are using the wrong variable in your new loop, and additionally creating a 3d array instead of a 2d array:

    for (int i = 0; i < m; i++)
    //                 ^^, should be n
    {
        tab[i] = new string[m];
        //                 ^^^
        // should be new string, not new string[m]
    }

vs:

    for (int i = 0; i < n; i++)
    //                 ^^, this one is correct
    {
        delete[] tab[i];
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Shouldn't it be n instead of m?
Actually it should also be n in the first loop, but after this change everything works. I missed this because I used m > n in my tests so my array had extra empty rows but could fit everything I tried to put inside. Thanks for a fast response.
@Kurigalzu Even with this change, your tablica2D class is easily broken by a two line main() program. { tablica2D t1(10, 10); tablica2D t2 = t1; } You failed to implement the "rule of 3" in your class.
0

If I need a C-like 2D array I always use:

type **myarr = new type*[X];
myarr[0] = new type[X*Y];
for (int i = 1; i < X; i++) {
    myarr[i] = myarr[0] + i * Y;
}

For usage:

myarr[x][y]

Then for freeing:

delete[] myarr[0];
delete[] myarr;

The same, with some little effort, can be applied for N-dimentional array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.