Results 1 to 5 of 5

Thread: QVector as function argument

  1. #1
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QVector as function argument

    http://www.daniweb.com/software-deve...threads/112482
    Does this also apply on Qvector?

    I pass large arrays (100000x100 double) many times (around 30000 times) and cant see the difference (in execution time) between
    Qt Code:
    1. f(QVector<QVector<double>>& _array);
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. f(QVector<QVector<double>> _array);
    To copy to clipboard, switch view to plain text mode 

    Thanks for your time, Stefan

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QVector as function argument

    QVector is implicit shared - so as long as you don't modify the QVector there is no deep copy if you pass by value, you can read more about implicit sharing here.

  3. #3
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector as function argument

    Quote Originally Posted by Zlatomir View Post
    QVector is implicit shared - so as long as you don't modify the QVector there is no deep copy if you pass by value, you can read more about implicit sharing here.
    Thank you for quick answer!
    So if I dont modify _array in function f - then array is passed by reference? Compiler decides that?

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QVector as function argument

    No, not the compiler, the class QVector is implemented in such a way that when you copy the QVector it doesn't copy all the data - it actually copy only a pointer and in increment the reference count variable (so that it can know if anybody else has access/share the same data) - this is implicit sharing (reference counting).

    And when you try to modify a QVector - it checks the reference count and if the object you try to modify is not the only one who shares the data it will copy the data and modify it's own copy (this is copy on write).

  5. The following 2 users say thank you to Zlatomir for this useful post:

    stefan (12th May 2011), TheIndependentAquarius (22nd March 2016)

  6. #5
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector as function argument

    wow.. .i didn't know that Qt is so smart
    Thanks!

Similar Threads

  1. argument parsing
    By deeee in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2010, 22:21
  2. Big problem with QVector::apend() function
    By Zander87 in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2010, 21:23
  3. QMap as function argument...
    By cydside in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 17:59
  4. Replies: 1
    Last Post: 12th January 2009, 18:05
  5. QtScript : passing array as argument into function
    By derek_r in forum Qt Programming
    Replies: 4
    Last Post: 27th October 2007, 10:46

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.