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. [solved] QByteArray::setRawData question(shallow copy or deep copy?)
Forum Updated to NodeBB v4.3 + New Features

[solved] QByteArray::setRawData question(shallow copy or deep copy?)

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.1k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    gilgil
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • G Offline
        G Offline
        gilgil
        wrote on last edited by
        #3

        ah... I didn't know that before.
        Thank you very much for your response. :)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          -1
          • G Offline
            G Offline
            gilgil
            wrote on last edited by
            #5

            Thank you again. ;)

            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