Hello to all, i have dynamic allocate memory for a vector but when i delete it i get an run time exception error.
Thanks.
Printable View
Hello to all, i have dynamic allocate memory for a vector but when i delete it i get an run time exception error.
Thanks.
Maybe you could be more especific... Show your code, for example.
Code:
#include <vector>
using namespace std;
int main()
{
vector<int>* obj = new vector<int>[5];
delete obj;
return 0;
}
I've done this before, and I did it like this:
Then to delete them, you would do this:Code:std::vector<int>* obj[5];
for (int i = 0; i < 5; i++)
obj[i] = new std::vector<int>;
Code:for (int i = 0; i < 5; i++)
delete obj[i];
Just out of curiousity, when would you want to use dynamic vector allocation?
Vectors are a VERY lightweight storage class.
You are using new[], but then using delete instead of delete[]. Frankly, I agree with Sharsnik: just write:
or:Code:#include <vector>
using namespace std;
int main()
{
vector<int> obj[5];
// ...
return 0;
}
Code:#include <vector>
#include <tr1/array> // Or #include whatever is appropriate.
using namespace std;
int main()
{
tr1::array<vector<int>, 5> obj;
// ...
return 0;
}
What a stupid mistake ?Quote:
You are using new[], but then using delete instead of delete[]. Frankly, I agree with Sharsnik: just write: