Hello!
Simple question: are the next two codes equal?
1:
char bytesReceivedTemp[SOCKET_BUFFER_SIZE];
//fill bytesReceivedTemp
bytesReceived.
append(QByteArray::fromRawData(bytesReceivedTemp,SOCKET_BUFFER_SIZE
));
QByteArray bytesReceived;
char bytesReceivedTemp[SOCKET_BUFFER_SIZE];
//fill bytesReceivedTemp
bytesReceived.append(QByteArray::fromRawData(bytesReceivedTemp,SOCKET_BUFFER_SIZE));
To copy to clipboard, switch view to plain text mode
2:
char bytesReceivedTemp[SOCKET_BUFFER_SIZE];
//fill bytesReceivedTemp
bytesReceived.append(bytesReceivedTemp,SOCKET_BUFFER_SIZE);
QByteArray bytesReceived;
char bytesReceivedTemp[SOCKET_BUFFER_SIZE];
//fill bytesReceivedTemp
bytesReceived.append(bytesReceivedTemp,SOCKET_BUFFER_SIZE);
To copy to clipboard, switch view to plain text mode
This question is related to that problem of doing a deep copy or having two objects sharing the same data. The QByteArray::fromRawData() states that it performs a "shallow copy" of bytesReceivedTemp's data to the returned QByteArray, needing the first one to be valid while using the second and only for reading (if a modification is attempted, Qt will make a deep copy of the data). Qt Assistant also says that when append() is used against a empty QByteArray (the above situation, different from QByteArray bytesReceived = "";), it also performs a "shallow copy" of the appended data instead of a deep one. It would seem, therefore, that the first code is quite redundant if it is performed only once, since it's performing a "shallow copy" of another "shallow copy", while the second is not (in a second iteration with a persistent (e.g. static) bytesReceived which isn't empty anymore would make the first code perform only one "shallow copy" while the second would make a deep one).
Am I correct in all what I sad? Which would be the smartest way of handling this in a embedded development situation, when "shallow copies" are preferred over deep ones, and if bytesReceived is handled as mentioned in the last parenthesis?
Thanks,
Momergil
Bookmarks