Exception thrown, access violation reading location while deleting 2d array in destructor

-1

I'll drop straight to the point. I have to practice making 2d arrays and deleting them succesfully while also practicing operator overloading. (I use Visual Studio Community 2017) I stumbled upon this problem: Program stops and I get this message:

Exception thrown at 0x0F13D9CB (ucrtbased.dll) in proba3.exe: 0xC0000005: Access violation reading location 0xDDDDDDCD.

On the console though, I get expected result, with one time use of the destructor. I have no idea what should I make of it, I scouted the internet but found nothing. Spot where it throws an error is commented.

class Tablica {
    int wiersze;
    int    kolumny;
    int ** tablica;
public:
    Tablica(int x, int y) {
        wiersze = x;
        kolumny = y;
        tablica = new int *[wiersze];
        for (int i = 0; i < wiersze; i++) {
            tablica[i] = new int [kolumny] {};
        }
    }

friend ostream & operator<<(ostream & wyjscie,const Tablica tab) {
        for (int i = 0; i < tab.wiersze; i++) {
            for (int j = 0; j < tab.kolumny; j++) {
                wyjscie << "\nWartosc [" << i << "][" << j << "] elementu tablicy wynosi: " << tab.tablica[i][j];
            }
        }
        return wyjscie;
    }

    ~Tablica() {
        for (int i = 0; i < wiersze; i++) {
            delete[] tablica[i]; ///////////////////////////////// error
        }
        delete[] tablica;
        cout << "\nUsunieto tablice";
    }
}; 
int main()
{
    cout << "Podaj liczbe wierszy: ";
    int a, b;
    cin >> a;
    cout << "Podaj liczbe kolumn: ";
    cin >> b;
    Tablica tab1(a,b);
    cout << tab1;


    return 0;
} // also here I get "expected }" message even though no one is missing
c++
c++11
asked on Stack Overflow Feb 10, 2019 by Mikele

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0