Results 1 to 12 of 12

Thread: Sizeof wrong with structures ?????

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sizeof wrong with structures ?????

    Thanks.
    But the problem of types and endianness are going to be the same if I want to load - save a simple variable , isn't it ?

    If you want to load or save a long int to a binary file what would you do ? (Suposing my progr are going to be used on different platforms)

    And I dont find references to compile options for Mingw. Can you tell me a usefull help-doc ?

    Thanks. Really thank you very much.

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

    Default Re: Sizeof wrong with structures ?????

    Quote Originally Posted by tonnot View Post
    But the problem of types and endianness are going to be the same if I want to load - save a simple variable , isn't it ?
    It depends on the variable. But in general dumping the variable without any control to disk is not the best thing to do. You should decide whether you store data as big or little endian and perform the conversion before storing and after restoring the data.

    If you want to load or save a long int to a binary file what would you do ? (Suposing my progr are going to be used on different platforms)
    I would store it in a well defined format -- for example as a 32 bit unsigned big endian natural binary encoded value.

    And I dont find references to compile options for Mingw. Can you tell me a usefull help-doc ?
    MinGW is GCC. Most of what applies to GCC, applies to MinGW as well.
    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.


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

    Default Re: Sizeof wrong with structures ?????

    If I was storing lots of small amounts of data in a file (such as thousands of positions), then I would state the endian format in the file format specification and ensure that format regardless of platform. I would also never use sizeof() as I have been bitten before by that returning different values on different compilers/platforms. I ensure that each object can serialise/deserialise itself from any storage (either disk or memory)

    If the data format lent itself to it, or I thought I could be extending the format in the future, then I wouldn't use a binary format at all. The nightmare in supporting lots of different versions isn't worth the compactness of the file and processing speed advantages. So I'd use something like XML (possibly compressed if they turn out large and I knew I had plenty of ram to work with)

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sizeof wrong with structures ?????

    I have found information about the theme.
    Intel has little endian always ?
    It depends on the operating system ? In that case Windows has one and mac, unix, linux the other?
    ( I think I know the answer, " it depends ....".)

    So, the solution are to set endianness or use types that are going to use always the same endianness.
    I can't find usefull information about 'Can I and how set endianness for my prog" (using c++ -Qt + mingw), or 'using safe endianness types '
    I have found this link, but I dont understand it .... http://www.realtime.bc.ca/articles/endian-safe

    Any help doc about it would be appreciated
    Thanks
    Last edited by tonnot; 28th June 2011 at 07:23.

  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: Sizeof wrong with structures ?????

    Its the processor which specifies endianness as its the processor which ends up storing the values (upto 64-bits) in memory.

    However, you shouldn't need to worry about this, you should be storing variables in a known endian regardless of processor your application is working on.

    Eg. instead of:

    Qt Code:
    1. int a;
    2.  
    3. write(&a, sizeof(a));
    To copy to clipboard, switch view to plain text mode 

    you would use something like:

    Qt Code:
    1. typedef unsigned int ui32;
    2. typedef unsigned char uc8;
    3.  
    4. ui32 a;
    5. ui8 c;
    6.  
    7. c = a >> 24; write (&c, 1);
    8. c = a >> 16; write (&c, 1);
    9. c = a >> 8; write (&c, 1);
    10. c = a;
    11. write (&a, 1);
    To copy to clipboard, switch view to plain text mode 

    then you know you've just written out as MSB (big endian) and guarantee that it will always be MSB regardless. It also guarantees that you'll also always write out 32 bits, regardless if the host OS has a 16-bit int (of course there maybe other consequences of this if you actually use all 32-bits)
    Last edited by squidge; 28th June 2011 at 07:59.

Similar Threads

  1. convert from QString to char[sizeof(...)]
    By adamatic in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2011, 09:05
  2. Replies: 5
    Last Post: 23rd June 2011, 08:31
  3. What do I do wrong
    By Arif Bilgin in forum Newbie
    Replies: 12
    Last Post: 20th October 2010, 20:03
  4. i don't know what i do wrong..
    By Hardstyle in forum Newbie
    Replies: 2
    Last Post: 27th June 2010, 17:33
  5. which one is constant,sizeof(int) or sizeof(long)?
    By coralbird in forum Qt Programming
    Replies: 4
    Last Post: 20th March 2006, 11:01

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.