[Development] Is make sense to use QVarLengthArray instead of QByteArray/QVector ?
Thiago Macieira
thiago.macieira at intel.com
Fri Jul 4 18:51:49 CEST 2014
On Friday 04 July 2014 12:48:28 Denis Shienkov wrote:
> Hi all.
>
> I have plans to replace use of QByteArray, which is used as the container
> for work with еру WinAPI functions, for example on QVarLengthArray. But I
> am confused by the fact of that in guts of QtCore is still often used the
> QByteArray for this purpose.
When the array is of char, it makes sense.
Anyway, please note that QVarLengthArray is supposed to be used where you'd
use a real Variable Length Array. That means you usually don't resize it. You
can do it, but not usually.
> Whether there is a sense to transition to QVarLengthArray?
>
> PS: For example, I speak like about it:
> https://codereview.qt-project.org/#/c/89096/2/src/serialport/qserialportinfo
> _win.cpp
That example shows an interesting case to be careful with: you have a
QVarLengthArray of 256 wchar_t on the stack (1k), then you initialise it at a
minimum to 16384 wchar_t (128k) (the dummyBuffer).
This means that you're wasting 1k of stack space. The greatest advantage of
QVarLengthArray is that it can use the stack instead of the heap, which is
faster. If you're not going to ever use it, set its Prealloc size to zero.
So I agree with Konstantin's comments there: you have a preallocated size on
the stack, so *use* it where it makes sense.
Also be very careful about stack overflow. Don't use large QVarLengthArrays (>
1k of stack usage) in functions that can re-enter, will call out to user code
or to other functions with large stack usage.
And 128k is never acceptable for stack usage.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
More information about the Development
mailing list