CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #1
    Join Date
    Aug 2009
    Posts
    68

    STL vector on heap?

    Hello,

    If I create an STL vector, is the memory assigned on the heap or the stack?

    For example:

    Code:
    #include <vector>
    
    int main()
    {
        std::vector<int> my_vector(3);
        my_vector[0] = 1;
        my_vector[1] = 10;
        my_vector[2] = 100;
    
        return 0;
    }
    Are the contents of "my_vector" on the heap, or the stack? I haven't declared the vector using the "new" keyword, so for a normal object, it would be created on the stack. But because a vector is a STL object, does it behave differently?

    If the vector is created on the heap, then how do I free the memory? Do I follow the standard approach:

    Code:
    // Free up the space allocated to the vector
    delete &my_vector;
    // Or perhaps this?
    delete my_vector;
    If your answer to the first part is the the vector is created on the stack, then how do I in fact create it on the heap? Do I use :

    Code:
    std::vector<int>* my_vector = new std::vector<int>(3);
    Thanks for the help
    Last edited by karnavor; November 25th, 2009 at 12:43 PM.

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