Aquarius_Girl
Aquarius_Girl

Reputation: 22936

What is the difference between QByteArray and QList<unsigned char> or QVector<unsigned char>?

QByteArray holds individual bytes, and if we make a QList of unsigned chars that will also hold the individual bytes.

What is the reason QByteArray exists when there can made a QList<unsigned char>?
What is the difference between QByteArray and QList<unsigned char> or QVector<unsigned char>?

What points am I missing?

Upvotes: 0

Views: 1538

Answers (2)

Jaa-c
Jaa-c

Reputation: 5157

QList allocates memory on heap and does not guarantee any data locality, so there can be a performance difference between using QList and QByteArray.

From: http://doc.qt.io/qt-5/qlist.html#details

QVector should be your default first choice. QVector will usually give better performance than QList, because QVector always stores its items sequentially in memory, where QList will allocate its items on the heap unless sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. See the Pros and Cons of Using QList for an explanation.

QByteArray also provides convenience methods for working with bytes (and strings in general).

Upvotes: 1

Jeka
Jeka

Reputation: 1384

QByteArray is a usefull wrapper around char*. Suitable for streaming with QDataStream, string operations and other data management. From the docs you also can find that:

Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.

QList, at first is not linear(subsequent) in memory (you should use QVector), and have no such usefull API

Upvotes: 2

Related Questions