Results 1 to 17 of 17

Thread: problem with memcpy

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

    Default problem with memcpy

    hi everyone!
    this is the syntax of a memcpy as i know: void *memcpy( void *var, const void *var1, size_t count );
    Here both var and var1 are arrays.
    So instead of var1 can we use a structure. I mean to say can i copy all the fields of a structure to an array using memcpy?
    Can any one help me please?


    With regards,

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem with memcpy

    Hi,

    Yes you can, but this is not a Qt related question.
    Òscar Llarch i Galán

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

    Default Re: problem with memcpy

    memcpy simply copies bytes in memory, so yes, you can do this. Whether you should or not isn't so easily answered. If the object you're copying is pure data, there probably won't be any problems, but if there's stuff like pointers or references involved, or a class or struct that does something like reference counting, this could be a bad idea. You need to be very sure you understand the object you're attempting to copy, or at least be prepared for a long, unpleasant debugging slog if things go south.

  4. #4
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with memcpy

    Qt Code:
    1. struct MyStruct {
    2. int x;
    3. int y;
    4. };
    5. MyStruct s;
    6. char buff[ sizeof( MyStruct ) ];
    7. memcpy( buf, &s, sizeof( MyStruct ) );
    To copy to clipboard, switch view to plain text mode 
    isn't it ?

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

    Default Re: problem with memcpy

    borisbn: Almost, but 'buf' isn't defined in your example

    Please be aware that memcpy'ing in this fashion comes with a lot of caveats. If a structure contains pointers, it can cause lots of headaches, for example, as can silent padding of structures, which can be platform and compiler specific.

    So it's best not to copy structures with memcpy.

    Since you are using Qt, you are using C++, so use a copy constructor instead of memcpy.

  6. #6
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with memcpy

    squidge, when you're writing a code from pda ( like I did ) there could be some mistakes
    And, of course, it's a bad idea to use memcpy for structures copy. The only way you can do it, if you want to serialize it to ( for example ) send via network.

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

    Default Re: problem with memcpy

    Quote Originally Posted by borisbn View Post
    Qt Code:
    1. struct MyStruct {
    2. int x;
    3. int y;
    4. };
    5. MyStruct s;
    6. char buff[ sizeof( MyStruct ) ];
    7. memcpy( buf, &s, sizeof( MyStruct ) );
    To copy to clipboard, switch view to plain text mode 
    isn't it ?
    hi borisbn,
    Thanks for replying, i tried the way you told, but it's not working. Is there any way to copy the contents of a structure to an array(of any type)?

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

    Default Re: problem with memcpy

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    Yes you can, but this is not a Qt related question.
    Hi nyaw,
    Thanks for replying, i know it is only a simple c++ question, but i'm using it in my Qt program. And tried to copy it the way i have posted, but it's not working. So, well can you tell me how to copy the contents of a structure to an array?

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: problem with memcpy

    Quote Originally Posted by sattu View Post
    this is the syntax of a memcpy as i know: void *memcpy( void *var, const void *var1, size_t count );
    Here both var and var1 are arrays.
    I don't think so.

    sizeof( MyStruct )
    Is also wrong when copying objects. Use the size of the actual object being copied.

    Why don't you implement an assignment operator?

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

    Default Re: problem with memcpy

    Quote Originally Posted by tbscope View Post
    I don't think so.


    Is also wrong when copying objects. Use the size of the actual object being copied.

    Why don't you implement an assignment operator?
    Sorry tbscope,
    i couldn't get you. How can i assign all the fields of a structure to a character array just by assignment operator?

  11. #11
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: problem with memcpy

    First:

    Qt Code:
    1. void *
    To copy to clipboard, switch view to plain text mode 
    Is not a character array.

    A question. What is the ultimate goal to put an object into a "character array"? What are you trying to achieve?
    Please also define character array. Is this like a string?

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

    Default Re: problem with memcpy

    Quote Originally Posted by tbscope View Post
    First:

    Qt Code:
    1. void *
    To copy to clipboard, switch view to plain text mode 
    Is not a character array.

    A question. What is the ultimate goal to put an object into a "character array"? What are you trying to achieve?
    Please also define character array. Is this like a string?
    Well tbscope, leave that example which i gave there. Actually, the thing is that i have a structure and some fields in it. So, now i need to copy all the contents of the structure to any array (may be QByteArray or Character array). But well, i have not been able to do it upto now. Leave the problem of memcpy, because that's not working. Can you suggest any other way of doing this copy?

    With regards,

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

    Default Re: problem with memcpy

    Quote Originally Posted by sattu View Post
    Can you suggest any other way of doing this copy?
    If you don't care about the ultimate size of the serialization, use QDataStream. If you do care then use QDataStream and then compress the resulting array using qCompress(). You'll get a byte array that you can send via a network link and then reverse the operation to receive the original structure.
    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.


  14. The following user says thank you to wysota for this useful post:

    sattu (28th September 2010)

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

    Default Re: problem with memcpy

    Quote Originally Posted by wysota View Post
    If you don't care about the ultimate size of the serialization, use QDataStream. If you do care then use QDataStream and then compress the resulting array using qCompress(). You'll get a byte array that you can send via a network link and then reverse the operation to receive the original structure.
    O.k, fine. Actually my friend is trying with QDataStream, but is getting so many errors, due to lack of proper knowledge of exact syntax. Let me try more with it, and see what i get.

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

    Default Re: problem with memcpy

    Quote Originally Posted by wysota View Post
    If you don't care about the ultimate size of the serialization, use QDataStream. If you do care then use QDataStream and then compress the resulting array using qCompress(). You'll get a byte array that you can send via a network link and then reverse the operation to receive the original structure.
    Hi wysota!
    can you suggest me some qt examples where they have used the concept of QDataStream to serialize the required datas and then store it in a qbytearray?
    Because i'm not able to get the basic idea regarding the it's use, so an example would do much better. So, can you just send me some useful examples?

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

    Default Re: problem with memcpy

    You should overload << operator and let the structure serialise itself. That way if you use different compiler/architecture, you don't have to worry about how the structure is formed in memory.

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

    Default Re: problem with memcpy

    There is an example in QDataStream docs. It's using QFile as the carrier but you can also pass it a byte array pointer.
    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.


  19. The following user says thank you to wysota for this useful post:

    sattu (29th September 2010)

Similar Threads

  1. Replies: 11
    Last Post: 3rd April 2012, 04:51
  2. Save 4 Integers in a QByteArray without memcpy
    By GonzoFist in forum Newbie
    Replies: 3
    Last Post: 17th May 2010, 23:15

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
  •  
Qt is a trademark of The Qt Company.