Qbytearray memory

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

  • 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?

  • 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?

  • 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

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.