Results 1 to 4 of 4

Thread: problem with vector

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

    Default problem with vector

    Hi, it happen a weird thing; the first two cout (inside for) print right values: eg -2 -2 1 1 and 1 1 4 4; the second cout print two times the second element: 1 1 4 4 and 1 1 4 4; Why this? thanks..
    Qt Code:
    1. //list.count() is = 2 in my test case
    2. vector <GLfloat*> ppp;
    3. GLfloat* Pos = new GLfloat[4];
    4. for (uint i=0; i < list.count(); ++i) {
    5. node = list.item(i);
    6. xrett = node.toElement();
    7. QString stringPOS = xrett.attribute("pos");
    8. QStringList values( QStringList::split( ' ', stringPOS ) );
    9. for( uint y = 0; y < values.size(); y++ ) {
    10. Pos[ y ] = values[ y ].toFloat();
    11. cout << "Pos[y] " << Pos[y] << endl;
    12. }
    13. ppp.push_back(Pos);
    14. cout << "PPPPPP " << ppp[i][0] << " " << ppp[i][1] << " " << ppp[i][2] << endl;
    15. }
    16. for (int i=0; i < list.count() ; ++i)
    17. cout << "PPPPPP " << ppp[i][0] << " " << ppp[i][1] << " " << ppp[i][2] << endl;
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    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: problem with vector

    std::vector::push_back() doesn't copy the array, since it doesn't even know that you pass an array to it.

    It should be:
    Qt Code:
    1. vector <GLfloat*> ppp;
    2. for (uint i=0; i < list.count(); ++i) {
    3. GLfloat* Pos = new GLfloat[4]; // <---
    4. ...
    5. ppp.push_back(Pos);
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: problem with vector

    Quote Originally Posted by jacek
    std::vector:ush_back() doesn't copy the array, since it doesn't even know that you pass an array to it.

    It should be:
    Qt Code:
    1. vector <GLfloat*> ppp;
    2. for (uint i=0; i < list.count(); ++i) {
    3. GLfloat* Pos = new GLfloat[4]; // <---
    4. ...
    5. ppp.push_back(Pos);
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    I don't understand your explain; maybe in my code I put the same 'space of memory' into ppp??? (space of memory' ->last modified Pos value)
    Regards

  4. #4

    Default Re: problem with vector

    Quote Originally Posted by mickey
    I don't understand your explain; maybe in my code I put the same 'space of memory' into ppp??? (space of memory' ->last modified Pos value)
    push_back( _T src ) just copy object src with type _T by calling copy construct of _T,
    in your code, _T = GLfloat* , this is an integer of 4 bytes (32 bit mechine), so it is
    a primitive type, just an integer, copy constructor is just copy the value, then
    in your first for loop, all "pos" points to the same memory block, so push_back( pos )
    copy the same integer value ( the same pointer), hence all value in ppp point to the
    same memory block.

    however, allocate new block pos in for loop may cause memory leakage, you may
    deallocate pos before ppp is destructed, since destructor of vector would call destructor
    of GLfloar* , but this is integer, it doesn't free memory block

    best regards Lung Sheng Chien

    Department of mathematics,
    Tsing Hua university, R.O.C

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.