Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [QString] Implicit Sharing

[QString] Implicit Sharing

Scheduled Pinned Locked Moved Solved General and Desktop
qstringdeep copy
3 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HAWK0044
    wrote on last edited by HAWK0044
    #1

    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.

    ? 1 Reply Last reply
    0
    • H HAWK0044

      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.

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      1
      • H Offline
        H Offline
        HAWK0044
        wrote on last edited by
        #3

        Thank you a lot for quick and exact answer!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved