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. Qbytearray memory
Forum Updated to NodeBB v4.3 + New Features

Qbytearray memory

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.9k 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.
  • D Offline
    D Offline
    darkp03
    wrote on last edited by
    #1

    Hi everybody!
    I need some dynamic memory space to allocate raw data, and I considered using que QByteArray form.
    QByteArray has the resize option, that allows me to resize it during my program, every time i need to.

    My question is, how do i know if the resize was unseccesfull? If theres not enough space to allocate my information?

    If that cant be done, id like to do it as you usally do with C. Using lists.

    Ive noticed that on QT we dont have the "malloc" command, but we do have the "new" command.

    Same question here, how do i know if the "new" was unseccesfull?

    Is there any other way to do this?

    My program recieves information from an UDP socket. I want to store this information on my RAM, untill I press a "save" button, that will save all of the information on a binary file.

    The easier way i thought of doing this is with a qbytearray, rezising it each time a recieve a new UDP package. (but i have no way of knowing if the resize was unseccesfull)

    A little more complicated way is using lists. I know how to do them with malloc and free, but im not too familiar with new() and delete().

    Is there another way? am i doing something wrong?

    Thx in advance, im quite new to QT, maybe im skipping something obvious here.

    1 Reply Last reply
    0
    • _ Offline
      _ Offline
      _rmn
      wrote on last edited by
      #2

      Hi!

      Questions about new and delete operators is about C++, not Qt. If new operator fails then std::bad_alloc exception will throw.
      Also if QByteArray::resize(int) fails then it should throw std::bad_alloc exception too.

      About your program: do you recieve UDP packages using QUdpSocket?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        darkp03
        wrote on last edited by
        #3

        Yes, i use QUdpSocket, it works perfectly, and its quite easy to use.

        std::bad_alloc, is a signal? can it be connected to a slot?

        1 Reply Last reply
        0
        • _ Offline
          _ Offline
          _rmn
          wrote on last edited by
          #4

          No std::bad_alloc is exception. It is 'pure' C++. (read more: http://www.cplusplus.com/doc/tutorial/exceptions/ ). It would be good if you learn some C++ before Qt...

          About exception: you should catch it:
          @
          SomeObjectType *a;
          try {
          a = new SomeObjectType;
          } catch (std::bad_alloc &e) {
          // here you catch that new operator fails
          }
          @

          1 Reply Last reply
          0
          • _ Offline
            _ Offline
            _rmn
            wrote on last edited by
            #5

            I think there is no need to manually resize your QByteArray..
            you can use method QByteArray::append(const QByteArra &ba); and give to it result of QUdpSocket::readAll() as parameter.

            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