[QString] Implicit Sharing



  • Hello. In Qt doc there is line about QByteArray and QString implicit sharing. So the question is, will QString take the reference of the byte array allocated in the stack?

    I have following code, it's reading string from stream ( which is read from network), assuming string len always > 0 and < 8192

    QString CPacketReader::readString(QDataStream &s){
        qint32 len = readInt(s);
    
        char buf[8192] = {0};
        int read = s.readRawData(buf,len);
        return QString::fromLatin1(buf,read);
    }
    

    Thank you.


  • Moderators

    @HAWK0044 said:

    will QString take the reference of the byte array allocated in the stack?

    No. Your code is safe. The data from the char array will be copied when the QString object is created.



  • Thank you a lot for quick and exact answer!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.