Results 1 to 4 of 4

Thread: memcpy not working properly

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question memcpy not working properly

    hi everyone!
    I am using the statement memcpy inside a function with the following code:

    void extract(QByteArray buffer,unsigned long *number)
    {
    QByteArray byte;
    byte = buffer;
    memcpy(number,byte,sizeof(unsigned long));
    }


    when the function is called for the first time, the memcpy works fine. But it doesnot work from next time onwards. So, can any one please help me regarding this problem? what modifications are required to it or is there any other alternative to it?


    advance thank you,

  2. #2
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: memcpy not working properly

    You copy the data representation of the class QByteArray to long. You can not know how it is implemented (unless you look into source code) and you can not handle the problem of possible changes to QByteArray implementation in later releases.
    You also dont know wheter QByteArraycontains enough bytes to read from. You should check that in extract():
    Qt Code:
    1. sizeof(*number) <= buffer.size()
    To copy to clipboard, switch view to plain text mode 

    Now what you really might have wanted was copying the data contained in QByteArray. Right?
    Then you might either copy the bytes by yourself, using byte[]. Here some untested code for that:
    Qt Code:
    1. for (int i = 0; i < buffer.size(); ++i)
    2. {
    3. (char*)number[i] = buffer[i];
    4. }
    To copy to clipboard, switch view to plain text mode 

    Or you convert QByteArray into normal C-Array and use that with memcpy:
    Qt Code:
    1. memcpy(number,buffer.data(),sizeof(*number));
    To copy to clipboard, switch view to plain text mode 
    Last edited by thrall; 26th October 2010 at 08:43.

  3. The following user says thank you to thrall for this useful post:

    sattu (26th October 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: memcpy not working properly

    QByteArray is an implicitly shared object so the data it holds is not really inside the QByteArray structure so even copying the object would not really copy any of its data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: memcpy not working properly

    It strikes me that, even if this did what you think it does, it would not do anything useful. The memcpy() works every time you call it and will dutifully overwrite the first bytes of the local QByteArray data structure (byte). The QByteArray byte goes out of scope and is destroyed on the next line. QByteArray buffer will remain unchanged outside the function partly because you are working on a copy inside the function, and partly because the pass-by-value semantics guarantee this.

    Perhaps you meant something like:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. void extract(QByteArray &buffer, unsigned long *number)
    5. {
    6. // reserve enough room for the bytes of number
    7. buffer.resize(sizeof(unsigned long));
    8. // get a temporary pointer to the byte array data buffer
    9. char *data = buffer.data();
    10. // copy
    11. memcpy(data, number, sizeof(unsigned long));
    12. }
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16. QCoreApplication app(argc, argv);
    17.  
    18. unsigned long number = 0x12345678;
    19. qDebug() << ba.size() << ba.toHex();
    20. for (int i=0; i<8; ++i) {
    21. extract(ba, &number);
    22. qDebug() << ba.size() << ba.toHex();
    23. number <<= 4;
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 23rd February 2012, 11:13
  2. problem with memcpy
    By sattu in forum General Programming
    Replies: 16
    Last Post: 29th September 2010, 09:20
  3. tab order not working properly for radio buttons
    By netmat in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2010, 14:58
  4. ScrollZoomer not working properly..
    By Raghaw in forum Qwt
    Replies: 1
    Last Post: 30th October 2009, 06:51
  5. Replies: 1
    Last Post: 1st June 2006, 23:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.