[solved] QByteArray::setRawData question(shallow copy or deep copy?)
-
Is QByteArray::setRawData() , are bytes copied or not? I do now know very well the difference beteen test1() and test2() function listed below. :(
"http://www.gilgil.net/109039":http://www.gilgil.net/109039
[source]
@void test1()
{
char buf[] = "abcde";
QByteArray ba;
ba.setRawData(buf, sizeof(buf) - 1);
buf[0] = 'A';
qDebug() << buf;
qDebug() << ba;
}void test2()
{
char buf[] = "abcde";
QByteArray ba;
ba.setRawData(buf, sizeof(buf) - 1);
ba[0] = 'A';
qDebug() << buf;
qDebug() << ba;
}int main()
{
test1();
test2();
return 0;
}@[run]
@Abcde
"Abcde"
abcde
"Abcde"@ -
Hi and welcome to devnet,
setRawData doesn't copy anything.
In test1, you are modifying the original array, ba points to the same location so it will show the same data.
In test2, you are modifying the QByteArray which will provoke a copy of the data. QByteArray and other implicitly shared class are "copy on write".
Hope it helps
-
You're welcome !
Since your question is answered, please also update your thread title prepending [solved] so other forum users may know a solution has been found :)