mercredi 1 février 2017

Delete or delete[] over new int[][]

I know in general if you new an instance of an object or a primary data type, you use delete; if you allocate an array such as new int[10], you free the memory by delete[]. I just came across another source and find out that in C++11, you can new a multidimensional array like this:

auto arr = new int[10][10];

My question is: Should I use delete or should I use delete[]? I would say delete[] looks more correct for me, however, delete doesn't crash the following program:

#include <stdio.h>

int main() {
    for (int i = 0; i < 100000; i++) {
        cout << i << endl;
        auto ptr = new int[300][300][300];
        ptr[299][299][299] = i;
        delete ptr;  // both delete and delete[] work fine here
    }
    return 0;
}

Why is that?

Aucun commentaire:

Enregistrer un commentaire