-
March 21st, 2009, 05:23 AM
#1
[RESOLVED] Delete Vector Error
Hello to all, i have dynamic allocate memory for a vector but when i delete it i get an run time exception error.
Thanks.
Thanks for your help.
-
March 21st, 2009, 06:21 AM
#2
Re: Delete Vector Error
Maybe you could be more especific... Show your code, for example.
-
March 22nd, 2009, 12:03 AM
#3
Re: Delete Vector Error
Code:
#include <vector>
using namespace std;
int main()
{
vector<int>* obj = new vector<int>[5];
delete obj;
return 0;
}
Thanks for your help.
-
March 22nd, 2009, 12:21 AM
#4
Re: Delete Vector Error
I've done this before, and I did it like this:
Code:
std::vector<int>* obj[5];
for (int i = 0; i < 5; i++)
obj[i] = new std::vector<int>;
Then to delete them, you would do this:
Code:
for (int i = 0; i < 5; i++)
delete obj[i];
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
March 22nd, 2009, 01:10 AM
#5
Re: Delete Vector Error
Just out of curiousity, when would you want to use dynamic vector allocation?
Vectors are a VERY lightweight storage class.
-
March 22nd, 2009, 06:25 AM
#6
Re: Delete Vector Error
You are using new[], but then using delete instead of delete[]. Frankly, I agree with Sharsnik: just write:
Code:
#include <vector>
using namespace std;
int main()
{
vector<int> obj[5];
// ...
return 0;
}
or:
Code:
#include <vector>
#include <tr1/array> // Or #include whatever is appropriate.
using namespace std;
int main()
{
tr1::array<vector<int>, 5> obj;
// ...
return 0;
}
-
March 23rd, 2009, 03:12 AM
#7
Re: Delete Vector Error
You are using new[], but then using delete instead of delete[]. Frankly, I agree with Sharsnik: just write:
What a stupid mistake ?
Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|