PDA

View Full Version : QVector as function argument



stefan
12th May 2011, 11:41
http://www.daniweb.com/software-development/cpp/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
f(QVector<QVector<double>>& _array);
and

f(QVector<QVector<double>> _array);

Thanks for your time, Stefan

Zlatomir
12th May 2011, 11:46
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 (http://doc.qt.nokia.com/latest/implicit-sharing.html#implicitly-shared).

stefan
12th May 2011, 11:55
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 (http://doc.qt.nokia.com/latest/implicit-sharing.html#implicitly-shared).

Thank you for quick answer!
So if I dont modify _array in function f - then array is passed by reference? Compiler decides that?

Zlatomir
12th May 2011, 12:01
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).

stefan
12th May 2011, 12:40
wow.. .i didn't know that Qt is so smart :)
Thanks!