Results 1 to 7 of 7

Thread: Typecast of two structs

  1. #1
    Join Date
    Aug 2011
    Location
    Germany
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Typecast of two structs

    I'm struggeling about with the typecast of one struct into another.

    I want to do something like this:

    Qt Code:
    1. struct myFirstStruct
    2. {
    3. unsigned char c1;
    4. unsigned char c2;
    5. unsigned char c3;
    6. unsigned char c4;
    7. unsigned char c5;
    8. unsigned char carray1[6];
    9. };
    10.  
    11. struct mySecondStruct
    12. {
    13. unsigned char carray1[4];
    14. unsigned char c1;
    15. unsigned char carray2[6];
    16. };
    17.  
    18. ...
    19.  
    20. myFirstStruct s1;
    21. mySecondStruct s2;
    22.  
    23. s1 = (myFirstStruct)s2;
    To copy to clipboard, switch view to plain text mode 

    As you can see, both arrays are of equal size.

    But I always get an error, that this conversion can't be done.
    How can I do this?

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Typecast of two structs

    if your goal is to copy to data from s2 to s1, you can try memcpy(&s1,&s2,sizeof(s2));

  3. #3
    Join Date
    Aug 2011
    Location
    Germany
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Typecast of two structs

    As I reviewed my exisitng sources I regocnized that my sample source-code was a little different. I have a myFirstStruct pointer and want to set this pointer to an existing pointer of mySecondStruct:

    Qt Code:
    1. myFirstStruct *s1;
    2. mySecondStruct *s2;
    3.  
    4. // fill s2 with some data
    5.  
    6. s1 = (myFirstStruct*)s2;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Typecast of two structs

    Quote Originally Posted by Markus_AC View Post
    As I reviewed my exisitng sources I regocnized that my sample source-code was a little different. I have a myFirstStruct pointer and want to set this pointer to an existing pointer of mySecondStruct:

    Qt Code:
    1. myFirstStruct *s1;
    2. mySecondStruct *s2;
    3.  
    4. // fill s2 with some data
    5.  
    6. s1 = (myFirstStruct*)s2;
    To copy to clipboard, switch view to plain text mode 
    this code compiles well and works for me. what is the error for you?

  5. #5
    Join Date
    Sep 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Typecast of two structs

    Quote Originally Posted by Markus_AC View Post
    As I reviewed my exisitng sources I regocnized that my sample source-code was a little different. I have a myFirstStruct pointer and want to set this pointer to an existing pointer of mySecondStruct:

    Qt Code:
    1. myFirstStruct *s1;
    2. mySecondStruct *s2;
    3.  
    4. // fill s2 with some data
    5.  
    6. s1 = (myFirstStruct*)s2;
    To copy to clipboard, switch view to plain text mode 

    Hi,

    (myFirstStruct*) is the C way to cast. In C++ is the worst way to cast.
    Take a look to dynamic_cast, static_cast, const_cast and even the devil reinterpret_cast (something you have to avoid)

    Qt has it's own way to cast when you work with QObjects derivatives


    In any case, the code is wrong

    You are copying the pointers, not the data

    And after s1 = (myFirstStruct*)s2; the previus value pointed by s1 is managed by no one, therefore a memory leak.

    If you work with linux, take a look to valgrind

    You have also be carefull with automatic copy constructor and automic assignament method

    In this case, they are ok, but is better if you comment it explicity. Take a look to effective C++ from Scott Meyer


    In this example, you have to write...

    *s1 = *s2;


    This question is not a Qt question, try to look a diferent forum for this kind of questions (sorry, I cannot recomend you anyone)

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Typecast of two structs

    what you are trying to do is very bad anyway. You are basically just trying to get around type safety. And for no good reason - just getting from one byte buffer to another.

    And you should be careful about presuming struct size just from eyeballing the class def - size changes depending on what padding etc has been needed/used.

  7. #7
    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: Typecast of two structs

    As you can see, both arrays are of equal size.
    Actually, both structs might be the same size but that will depend on the way the compiler packs the structures: see Data Structure Alignment. You will probably get away with it most of the time if all the elements are chars. If you mix types then watch out:
    Qt Code:
    1. #include <iostream>
    2.  
    3. struct myFirstStruct
    4. {
    5. unsigned char c1;
    6. unsigned char c2;
    7. unsigned char c3;
    8. unsigned char c4;
    9. unsigned char c5;
    10. unsigned char carray1[6];
    11. };
    12.  
    13. struct mySecondStruct
    14. {
    15. unsigned char carray1[4]; // 4*1 byte
    16. unsigned char c1;
    17. unsigned char carray2[6];
    18. };
    19.  
    20. struct myThirdStruct
    21. {
    22. unsigned short sarray1[2]; // 2*2 bytes
    23. unsigned char c1;
    24. unsigned char carray2[6];
    25. };
    26.  
    27. int main(int argc, char **argv)
    28. {
    29. std::cout << sizeof(unsigned char) << " " << sizeof(unsigned short) << std::endl;
    30. std::cout << sizeof(myFirstStruct) << " " << sizeof(mySecondStruct) << " " << sizeof(myThirdStruct) << std::endl;
    31. return 0;
    32. }
    To copy to clipboard, switch view to plain text mode 
    outputs:
    Qt Code:
    1. 1 2
    2. 11 11 12
    To copy to clipboard, switch view to plain text mode 
    To do this cast safely requires an explicit copy constructor.

Similar Threads

  1. Structs for C++ and Qt
    By therealjag in forum General Programming
    Replies: 8
    Last Post: 6th June 2011, 11:56
  2. C++ friend classes, with structs and enums
    By kachofool in forum General Programming
    Replies: 4
    Last Post: 3rd December 2009, 15:03
  3. typecast of QWidget
    By wagmare in forum Qt Programming
    Replies: 5
    Last Post: 11th August 2009, 12:12
  4. structs, pointer, static callback function
    By vonCZ in forum General Programming
    Replies: 3
    Last Post: 20th June 2008, 12:53

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.