Results 1 to 12 of 12

Thread: <vector> and new

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: <vector> and new

    Quote Originally Posted by mickey
    Hi I'm trying to do this (and it seem works):

    Qt Code:
    1. std::vector<Light*> light; // it's a vector of pointer to Light
    2. Light* l;
    3. l = new Light[10];
    4. light.push_back(l);
    5. delete [] l; //delete is necessary
    To copy to clipboard, switch view to plain text mode 
    Here you use that vector as if it was a vector of arrays, but there is one problem --- if you delete the "l" pointer, the pointer you have appended to the vector will be invalid, because it will point to a deleted array.

    It should be:
    Qt Code:
    1. std::vector<Light*> light;
    2. Light* l;
    3. l = new Light[10];
    4. light.push_back(l);
    5.  
    6. // ... use light ...
    7.  
    8. // delete the contents of light vector
    9. for( std::vector<Light*>::iterator i = light.begin(); i != light.end(); ++i ) {
    10. delete [] *i;
    11. }
    12. lisht.clear();
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by mickey
    Otherwise:
    Qt Code:
    1. std::vector<Light*> light; // it's a vector of pointer to Light
    2. Light* l;
    3. l = new Light;
    4. for (int i=0; < 10< i++)
    5. light.push_back(l);
    6. delete [] l; //delete is necessary
    To copy to clipboard, switch view to plain text mode 
    Here you use the same variable as if it was a vector of pointers to Light instance, but again there is a problem --- all elements point to the same instance, if you delete it, all pointers in the vector will be invalid.

    Qt Code:
    1. std::vector<Light*> light; // it's a vector of pointer to Light
    2.  
    3. for (int i=0; < 10< i++) {
    4. Light* l = new Light();
    5. light.push_back(l);
    6. // or simply:
    7. // light.push_back( new Light() );
    8. }
    9.  
    10. // ... use light ...
    11.  
    12. // delete the contents of light vector
    13. for( std::vector<Light*>::iterator i = light.begin(); i != light.end(); ++i ) {
    14. delete *i; // note no []
    15. }
    16. lisht.clear();
    To copy to clipboard, switch view to plain text mode 

    Another problem is that you can't use delete[] to free memory allocated with new --- always use the same kind of operator: delete after new and delete [] after new[].

    Quote Originally Posted by mickey
    Here I've a vecotr of 10 elements (pointer) that each one point to a Light;
    Qt Code:
    1. std::vector<Light> light;
    2. for (int i=0; <10; i++)
    3. light.push_back(Light());
    To copy to clipboard, switch view to plain text mode 
    I prefer the last....
    Wich is better?
    This is the safest way to use a vector.

    Note that you can achieve the same with this:
    Qt Code:
    1. std::vector<Light> light;
    2. light.resize( 10 );
    3. // or even
    4. // std::vector<Light> light( 10 );
    To copy to clipboard, switch view to plain text mode 
    It will fill the vector with 10 copies of Light().

  2. The following user says thank you to jacek for this useful post:

    mickey (18th May 2006)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.