CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    [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.

  2. #2
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: Delete Vector Error

    Maybe you could be more especific... Show your code, for example.

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  4. #4
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    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

  5. #5
    Join Date
    Sep 2007
    Posts
    49

    Re: Delete Vector Error

    Just out of curiousity, when would you want to use dynamic vector allocation?

    Vectors are a VERY lightweight storage class.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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
  •  





Click Here to Expand Forum to Full Width

Featured