Results 1 to 12 of 12

Thread: <vector> and new

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default <vector> and new

    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 
    I I understand fine, this is a vector of Light with only one element(a pointer) that point to an array of 10 Light. Is it this?
    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 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? Thanks
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: <vector> and new

    But these are different. The first one stores arrays of objects and the last one stores objects -- the first one is twodimensional whereas the last one is onedimensional.

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

    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().

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

    mickey (18th May 2006)

  5. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: <vector> and new

    Anther question: the vector <Light> is member of a Class A; I putted only delete [] l inside the destructor of A; Is it stiil necessary the vector destructor as you write and clear()? If I destruct the class that contatin vector....
    Thanks
    Regards

  6. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: <vector> and new

    Furthermore in the first case: coding delete [] *i; Are you deleting the same space of memory more time? There's only one array of Light[10]....
    Regards

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

    Default Re: <vector> and new

    Quote Originally Posted by mickey
    Anther question: the vector <Light> is member of a Class A; I putted only delete [] l inside the destructor of A; Is it stiil necessary the vector destructor as you write and clear()? If I destruct the class that contatin vector....
    No, you don't have to --- the vector will clear itself when you destroy it.

    Quote Originally Posted by mickey
    Furthermore in the first case: coding delete [] *i; Are you deleting the same space of memory more time? There's only one array of Light[10]....
    If there is only one array, you don't need the vector.

  8. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: <vector> and new

    Hence, if I code as below is clear() necessary? Thanks
    Qt Code:
    1. class Scene {
    2. std::vector<Light> light;
    3. };
    4. Scene::Scene() {
    5. for (int i=0; <10; i++)
    6. light.push_back(Light());
    7. }
    8.  
    9. Scene::~Scene() {
    10. light.clear(); // necessary?
    11. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: <vector> and new

    The vector will clear itself when it's destructed so an explicit clear() is not necessary (and remember clear() doesn't free the memory occupied by items stored in the vector).

  10. The following user says thank you to wysota for this useful post:

    mickey (18th May 2006)

  11. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: <vector> and new

    Sorry I've still compile problem; the first compile (and seem works too), the 2th no;Why? What is happening?
    Qt Code:
    1. class Scene {
    2. std::vector<Light> light;
    3. };
    4.  
    5. Scene::Scene() {
    6. light.push_back(Light());
    7. }
    8.  
    9. Scene::~Scene() {
    10. light.clear();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Scene {
    2. std::vector<Light> light(1); //with (1) don't compile
    3. };
    4.  
    5. Scene::Scene() {
    6. light.push_back(Light());
    7. }
    8.  
    9. Scene::~Scene() {
    10. light.clear();
    11. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. qsignalslotimp.h(66): fatal error C1903: unable to recover from previous error(s); stopping compilation
    2. mainform.ui.h(75): error C2109: subscript requires array or pointer type
    3. mainform.ui.h(75): error C2228: left of '.getPosition' must have class/struct/union type
    4. mainform.ui.h(75): error C2475: 'Scene::light' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
    5. mainform.ui.h(75): error C2475: 'Scene::light' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
    6. mainform.ui.h(959): error C2109: subscript requires array or pointer type
    7. mainform.ui.h(959): error C2228: left of '.setXPosition' must have class/struct/union type
    8. ..........................................
    To copy to clipboard, switch view to plain text mode 
    Last edited by mickey; 18th May 2006 at 15:35.
    Regards

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

    Default Re: <vector> and new

    It should be:
    Qt Code:
    1. class Scene
    2. {
    3. public:
    4. Scene();
    5. // ...
    6. private:
    7. std::vector<Light> light;
    8. };
    9.  
    10. Scene::Scene() : light(1)
    11. {
    12. // ...
    13. }
    To copy to clipboard, switch view to plain text mode 

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

    mickey (18th May 2006)

  14. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: <vector> and new

    OK It works! but I don't understand where's the problem in my solutions.. Can't I give dimension of vector inside class? thanks
    Regards

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

    Default Re: <vector> and new

    Quote Originally Posted by mickey
    Can't I give dimension of vector inside class?
    No, you can't. You can only declare member variables there.

  16. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.