Results 1 to 8 of 8

Thread: a question regarding QByteArray

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default a question regarding QByteArray

    Hi to all,
    (wysota sorry in advance if my question is trivial ).
    I would know why these 2 piece of code give different result:

    Qt Code:
    1. QByteArray ba = m_axobj->dynamicCall("GetUserData(QString)", id ).toByteArray();
    2. quint16 templatesize = 0;
    3. memcpy( &templatesize, &ba[87], sizeof(quint16) );
    4. templatesize = qFromBigEndian(templatesize);
    5. qDebug() << templatesize;
    To copy to clipboard, switch view to plain text mode 

    And

    Qt Code:
    1. QByteArray ba = m_axobj->dynamicCall("GetUserData(QString)", id ).toByteArray();
    2. const char *dat = ba.constData();
    3. quint16 templatesize = 0;
    4. memcpy( &templatesize, dat+87, sizeof(quint16) );
    5. templatesize = qFromBigEndian(templatesize);
    6. qDebug() << templatesize;
    To copy to clipboard, switch view to plain text mode 

    Reading QByteArray documentation the [i] operator should return the byte at position i so with the & operator I get its address.
    This should be the same of adding the offset i at the first value address.
    I'm sure I'm wrong but where?
    Franco Amato

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: a question regarding QByteArray

    Isn't the [] an overloaded operator in the QByteArray class? If you want to get a pointer to a sequential array of the bytes, you should use the constData or equivalent member functions.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a question regarding QByteArray

    Quote Originally Posted by fatjuicymole View Post
    Isn't the [] an overloaded operator in the QByteArray class?
    Yes is it.

    If you want to get a pointer to a sequential array of the bytes, you should use the constData or equivalent member functions.
    But teorically the 2 code should give the same address as

    Qt Code:
    1. int vet[10] = {1,2,3,4,5,6,7,8,9,0};
    2.  
    3. &vet[5] and
    4. vet+5
    To copy to clipboard, switch view to plain text mode 
    should give the same address or I'm wrong somewhere?
    Franco Amato

  4. #4
    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: a question regarding QByteArray

    Quote Originally Posted by franco.amato View Post
    (wysota sorry in advance if my question is trivial ).
    If it's trivial then post it in the newbie forum.

    BTW. Check if sizeof(quint16)==sizeof(char) and then think what it means.
    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. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: a question regarding QByteArray

    Quote Originally Posted by franco.amato View Post
    Yes is it.
    If so then check the signature, declaration and description in docs. I can help you:
    QByteRef operator[] ( int i )
    char operator[] ( int i ) const
    QByteRef operator[] ( uint i )
    char operator[] ( uint i ) const
    As you can see operator[] is not returning reference, but copy. So if it is a copy then it is somewhere else in memory then the actual value in the internal array (and yes, it is rather basic knowledge - if you are not sure how returning a copy, reference and pointer differs make some class with some message printing in copy constructor and return it from any function in different ways so you can see how many times an instance of a class was copied - useful excercise - I'm making such similar excericises quite often as sometimes I'm not sure how something works and I helps me to understand).

    But teorically the 2 code should give the same address as

    Qt Code:
    1. int vet[10] = {1,2,3,4,5,6,7,8,9,0};
    2.  
    3. &vet[5] and
    4. vet+5
    To copy to clipboard, switch view to plain text mode 
    should give the same address or I'm wrong somewhere?
    For C style array it is true. But you are using QByteArray class and not C array, so operator[] can do anything (it can even always return letter 'a' independently of given index - why not? )
    Last edited by faldzip; 29th April 2010 at 22:50.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a question regarding QByteArray

    Thank you very much.
    Franco Amato

  7. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: a question regarding QByteArray

    Reading QByteArray documentation the [i] operator should return the byte at position i so with the & operator I get its address.
    This should be the same of adding the offset i at the first value address.
    No, the documentation makes no such guarantee. Just because they've overloaded the indexing operator to make it look like a C-style array doesn't mean it is a C-style array. In fact, there is no way of telling how the data in such an object is actually stored simply by looking at the class interface. In fact, given the mention of the use of shared memory in the class documentation, it's nearly certain that taking brute-force addresses as you are doing will fail to behave anything like such tactics would with a simple C array.

  8. #8
    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: a question regarding QByteArray

    Quote Originally Posted by faldżip View Post
    so operator[] can do anything (it can even always return letter 'a' independently of given index - why not? )
    It can even return 42
    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.


Similar Threads

  1. Regarding qbytearray
    By mohanakrishnan in forum Qt Programming
    Replies: 7
    Last Post: 19th November 2009, 13:38
  2. QByteArray to new QDomDocument
    By cknoblock in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2009, 21:37
  3. Replies: 9
    Last Post: 25th July 2009, 13:27
  4. QByteArray
    By gyre in forum Newbie
    Replies: 4
    Last Post: 9th October 2007, 18:30
  5. QByteArray in Qt3
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 06:16

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.