Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Solved] QByteArray and dynamic memory
QtWS25 Last Chance

[Solved] QByteArray and dynamic memory

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 2.5k 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.
  • McLionM Offline
    McLionM Offline
    McLion
    wrote on last edited by
    #1

    Hi
    Some basic understanding question:
    I know that the following gives me exactly the memory I need:
    @QByteArray * datagram;
    datagram = new(std::nothrow) QByteArray [uDataSize.l];
    @

    On other places I simply use:
    @QByteArray RawPortData;
    @

    which works as supposed (where I actually don't know how much I need at runtime).
    How much memory does Qt/++ reserve in the second case?
    Do I need to allocate with new?

    Thanks
    McL

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      [quote author="McLion" date="1390826819"]
      I know that the following gives me exactly the memory I need:
      @QByteArray * datagram;
      datagram = new(std::nothrow) QByteArray [uDataSize.l];
      @
      [/quote]No, this won't give you exactly the memory you need. This will create many empty arrays.

      [quote]On other places I simply use:
      @QByteArray RawPortData;
      @

      which works as supposed (where I actually don't know how much I need at runtime).
      How much memory does Qt/++ reserve in the second case?
      Do I need to allocate with new?[/quote]This is the correct way to use QByteArray. You should virtually never need to allocate Qt containers (QByteArray, QString, QVector, etc.) using new.

      Qt keeps track of the memory allocated. If you add more data into the array, Qt will automatically allocate more memory.

      See also http://qt-project.org/doc/qt-5/QByteArray.html

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • McLionM Offline
        McLionM Offline
        McLion
        wrote on last edited by
        #3

        OK.
        Thanks a lot.

        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