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. Is the following assignment operation on a QByteArray reference safe?
Forum Updated to NodeBB v4.3 + New Features

Is the following assignment operation on a QByteArray reference safe?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 335 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.
  • S Offline
    S Offline
    schrute
    wrote on last edited by
    #1

    I have a function that takes a QByteArray object as input by reference and assigns another object inside, like the following

    Result myfunc(QByteArray& bytes)
    {
        ....
        bytes = QByteArray(rawData, rawDataSize);
        ....
    }
    
    
    QByteArray byteArray;
    myfunc(byteArray);
    parse(byteArray);
    

    Is this assignment operation within myfunc safe? I would expect it to be a safe operation for a std::vector but I am not sure about QByteArray (shallow-copy and deep-copy issues).

    PS - I would have preferred a return by value but can't change the API

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      I don't see a problem here. What should be the problem? If copy on write would change such fundamental things for the user/programmer than it would not be used by Qt. You don't need to think about cow in 99,99% of the time

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply
      0
      • S schrute has marked this topic as solved on
      • Christian EhrlicherC Christian Ehrlicher

        I don't see a problem here. What should be the problem? If copy on write would change such fundamental things for the user/programmer than it would not be used by Qt. You don't need to think about cow in 99,99% of the time

        S Offline
        S Offline
        schrute
        wrote on last edited by
        #3

        @Christian-Ehrlicher I incorrectly assumed that QByteArray would always create a deep copy of the data which was not true for QByteArray::fromRawData (documented behaviour, that I didn't read). So just wanted to confirm if this would behave as intended.

        1 Reply Last reply
        1

        • Login

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