When I run the code:
#include <iostream>
#include <vector>
using namespace std;
struct A_s {
int *arr;
unsigned int l;
A_s(unsigned int size) : l(size) {
arr = new int[size];
cout<<'\t'<<arr<<endl;
}
~A_s() {
delete[] arr; // works if line is removed
}
};
vector<A_s> vec;
int main() {
cout<<"vec 0"<<endl;
vec.emplace_back(1);
cout<<"vec 1"<<endl;
vec.emplace_back(1);
cout<<"vec 2"<<endl;
vec.emplace_back(1);
for(unsigned int i = 0; i < vec.size(); i++) cout<<&vec[i]<<endl;
cout<<"end"<<endl;
return 0;
}
The output is:
vec 0
0x761710
vec 1
0x761a90
vec 2
0x761ab0
mingw32-make: *** [Makefile:6: run] Error -1073740940
The program runs without errors if I delete the line delete[] arr
. Is the destructor being called before the end of the program? And even if it does, why is it causing an error?