Results 1 to 7 of 7

Thread: Question about qvector.h code of Qt 4

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Question about qvector.h code of Qt 4

    Hi all!
    While I was looking at the code of qvector.h (the header file of QVector class - I use Qt 4.2.2) and I noticed something which I don't understand. For example, here is the implementation of the QVector:append() method:

    Qt Code:
    1. template <typename T>
    2. void QVector<T>::append(const T &t)
    3. {
    4. const T copy(t);
    5. if (d->ref != 1 || d->size + 1 > d->alloc)
    6. realloc(d->size, QVectorData::grow(sizeof(Data), d->size + 1, sizeof(T),
    7. QTypeInfo<T>::isStatic));
    8. if (QTypeInfo<T>::isComplex)
    9. new (d->array + d->size) T(copy);
    10. else
    11. d->array[d->size] = copy;
    12. ++d->size;
    13. }
    To copy to clipboard, switch view to plain text mode 

    So my question is:
    What's the use of the copy variable? Is it there for a reason or is it redundant?

    Would the following code:

    Qt Code:
    1. template <typename T>
    2. void QVector<T>::append(const T &t)
    3. {
    4. // const T copy(t);
    5. if (d->ref != 1 || d->size + 1 > d->alloc)
    6. realloc(d->size, QVectorData::grow(sizeof(Data), d->size + 1, sizeof(T),
    7. QTypeInfo<T>::isStatic));
    8. if (QTypeInfo<T>::isComplex)
    9. new (d->array + d->size) T(t);
    10. else
    11. d->array[d->size] = t;
    12. ++d->size;
    13. }
    To copy to clipboard, switch view to plain text mode 

    do the same thing as the original code?

    The same practice seems to be used in other functions of QVector as well like in QVector::fill():

    Qt Code:
    1. template <typename T>
    2. QVector<T> &QVector<T>::fill(const T &from, int asize)
    3. {
    4. const T copy(from); //is it really needed???
    5. resize(asize < 0 ? d->size : asize);
    6. if (d->size) {
    7. T *i = d->array + d->size;
    8. T *b = d->array;
    9. while (i != b)
    10. *--i = copy;
    11. }
    12. return *this;
    13. }
    To copy to clipboard, switch view to plain text mode 

    and probably in other classes too.

    So, is this practice of creating a const copy (as an automatic variable) redundant or am I missing something???

  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: Question about qvector.h code of Qt 4

    Consider such code:
    Qt Code:
    1. QVector<int> v;
    2. ...
    3. v.append( v[0] );
    To copy to clipboard, switch view to plain text mode 
    What will happen if realloc() moves the data?

  3. #3
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question about qvector.h code of Qt 4

    So why pass t by reference in the first place then?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  4. #4
    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: Question about qvector.h code of Qt 4

    Quote Originally Posted by Michiel View Post
    So why pass t by reference in the first place then?
    Who knows? Maybe this problem was noticed after the interface was set or maybe to leave a possibility for more efficient implementation in future or for some other reason. You will have to ask the Trolls.

    Anyway now it's just like if parameters were passed by value.

  5. #5
    Join Date
    Aug 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Question about qvector.h code of Qt 4

    Thanks for the replies guys! jacek you are right! I did not notice that such a thing could happen in the first place! I 've just noticed that the same code exists in STL <vector> as well and there is an enlightening comment:

    Qt Code:
    1. _Ty _Tmp = _Val; // in case _Val is in sequence
    To copy to clipboard, switch view to plain text mode 

    Thanks again!

  6. #6
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question about qvector.h code of Qt 4

    Hm. Perhaps it is to ensure that the type has a public copy constructor? I'm not sure if a pass-by-value parameter would ensure the same thing.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  7. #7
    Join Date
    Aug 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about qvector.h code of Qt 4

    Quote Originally Posted by Michiel View Post
    Hm. Perhaps it is to ensure that the type has a public copy constructor? I'm not sure if a pass-by-value parameter would ensure the same thing.
    I think that pass-by-value parameter does exactly the same thing i.e it calls the copy constructor to create a copy of the passed object.


    I think that an optimazation would be to create the copy only when resize (i.e call realloc) is needed. This would possibly result in a bit larger code but it would probably be better not to needlessly invoke the copy constructor in case that large objects are stored inside the QVector (for example images)

Similar Threads

  1. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15
  2. Example code and metaObject question
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 12th January 2007, 18:34
  3. a question conerning code in main.cpp
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2006, 16:55
  4. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41

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.