Results 1 to 16 of 16

Thread: system-independent C++ data types

  1. #1
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question system-independent C++ data types

    Anyone know a good way in C++ of defining numeric variables in system-independent manner such as:
    32-bit integer
    64-bit real
    16-bit unsigned integer,
    etc.

    ... purpose is reading and writing binary data to file so that, for example, 32-bit Windows user will always be able to read the file I wrote with 64-bit Linux, and vice versa.

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

    Default Re: system-independent C++ data types

    Some libraries (like Qt) define their own platform independent types. For instance qint32, qint64, etc.

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

    magland (24th March 2007)

  4. #3
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: system-independent C++ data types

    Thanks for the info Wysota... Qt is amazing has so many things, yet always so simple...

    Any ideas for 32- and 64- bit floating point? I noticed there is no qreal32 or qfloat32 for example in Qt.

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

    Default Re: system-independent C++ data types

    First question would be - do you really need to know if a double is 64bit or 128bit long? I sincerily doubt you'll ever need a 128bit precision

  6. #5
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: system-independent C++ data types

    Quote Originally Posted by wysota View Post
    First question would be - do you really need to know if a double is 64bit or 128bit long? I sincerily doubt you'll ever need a 128bit precision
    I see your point, and agree in most situations. However the pitfall is reading and writing to file.

    For example, suppose I have the following code:

    Qt Code:
    1. void write_raw_data(FILE *outf,double *data, long N) {
    2. fwrite(data, sizeof(double),N,outf);
    3. }
    4.  
    5. void read_raw_data(FILE *inf,double *data, long N) {
    6. fread(data, sizeof(double),N,inf);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Then, you may not be able to read the file I wrote, and vice versa.

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

    Default Re: system-independent C++ data types

    In that case the type size won't help you. If I have a big endian machine and you have a little endian machine (for instance a PPC Mac and a x86 PC) you'll get completely different values from the same binary representation. When you want to store a value in a file, you should always store it the same way, so that if you read it, you can be sure it is read properly - for example using a big endian 32 bit long U2 encoded representation of an integer. Then it's only a matter of reading the value the exact same way and interpretting it correctly.

  8. #7
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: system-independent C++ data types

    Quote Originally Posted by wysota View Post
    In that case the type size won't help you. If I have a big endian machine and you have a little endian machine (for instance a PPC Mac and a x86 PC) you'll get completely different values from the same binary representation. When you want to store a value in a file, you should always store it the same way, so that if you read it, you can be sure it is read properly - for example using a big endian 32 bit long U2 encoded representation of an integer. Then it's only a matter of reading the value the exact same way and interpretting it correctly.
    Okay, so lets say I have an integer ( int x=5; ). What C++ commands would I need to perform in order to write it to a file using say "big endian 32-bit long U2 representation". Would I have to create the individual bytes one by one?

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

    Default Re: system-independent C++ data types

    use the htonl() function, it converts a "host" integer to "network" integer.

  10. #9
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: system-independent C++ data types

    Quote Originally Posted by wysota View Post
    use the htonl() function, it converts a "host" integer to "network" integer.
    What the point here??? Wasn't the question about "file" writing? In this case QDataStream and QTextStream are basically everything one needs.
    Current Qt projects : QCodeEdit, RotiDeCode

  11. The following user says thank you to fullmetalcoder for this useful post:

    magland (28th March 2007)

  12. #10
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: system-independent C++ data types

    Quote Originally Posted by fullmetalcoder View Post
    What the point here??? Wasn't the question about "file" writing? In this case QDataStream and QTextStream are basically everything one needs.
    Yes, thanks. I think that's right... I will use QDataStream for writing raw data. It has everything I want including "setByteOrder".

    Now my only remaining question is does the following code always write a 64-bit quantity, even on systems where double is 128 byte?

    Qt Code:
    1. QDataStream & operator<< ( double f )
    To copy to clipboard, switch view to plain text mode 

    According to the Qt documentation, this should always be 64-bit output, but I'm skeptical... does anyone have an idea about it?

  13. #11
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: system-independent C++ data types

    Quote Originally Posted by magland View Post
    Now my only remaining question is does the following code always write a 64-bit quantity, even on systems where double is 128 byte?

    Qt Code:
    1. QDataStream & operator<< ( double f )
    To copy to clipboard, switch view to plain text mode 
    According to the Qt documentation, this should always be 64-bit output, but I'm skeptical... does anyone have an idea about it?
    1. Do you know many systems where double occupy 128 bytes?
    2. If the docs clearly state that a 64 byte float is written then why being skeptical... Qt docs are by far the best I've ever seen in a C++ library so lets trust or, if you're still unsure, ask the Trolls (or check out the sources)...
    3. After verification it seems that there is a *STANDARD* for double type : "IEEE 754", according to the docs. What about googling around for some specs? http://en.wikipedia.org/wiki/IEEE_754
    Current Qt projects : QCodeEdit, RotiDeCode

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

    magland (28th March 2007)

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

    Default Re: system-independent C++ data types

    Quote Originally Posted by fullmetalcoder View Post
    What the point here??? Wasn't the question about "file" writing? In this case QDataStream and QTextStream are basically everything one needs.
    Provided that one uses Qt. The question is in the "General Programming" forum. If one doesn't use Qt, htonl, ntohl, htons and ntohs are nice macros to convert between host and network (big endian) ordered integers.

    Do you know many systems where double occupy 128 bytes?
    Probably every 64b system has 128b long doubles. They are not called "doubles" for nothing.

  16. #13
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: system-independent C++ data types

    Quote Originally Posted by wysota View Post
    Provided that one uses Qt. The question is in the "General Programming" forum. If one doesn't use Qt, htonl, ntohl, htons and ntohs are nice macros to convert between host and network (big endian) ordered integers.
    True enough but it seems Qt fits everywhere...

    Quote Originally Posted by wysota View Post
    Probably every 64b system has 128b long doubles. They are not called "doubles" for nothing.
    I don't think so... I mean, not every 64b system has 128b doubles... They aren't indeed called "doubles" for nothing... Yet there also exist "quads" (though not widely used right now because brought by a recent revision of the aforementioned IEEE standard : http://en.wikipedia.org/wiki/IEEE_754r). Ain't the world so nice?
    Last edited by fullmetalcoder; 28th March 2007 at 18:20. Reason: spelling error
    Current Qt projects : QCodeEdit, RotiDeCode

  17. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: system-independent C++ data types

    Quote Originally Posted by wysota View Post
    Probably every 64b system has 128b long doubles. They are not called "doubles" for nothing.
    (128b long) doubles or 128b (long doubles)?
    Qt Code:
    1. #include <iostream>
    2.  
    3. int main()
    4. {
    5. std::cout << "int = " << sizeof( int ) << std::endl;
    6. std::cout << "int * = " << sizeof( int * ) << std::endl;
    7. std::cout << "float = " << sizeof( float ) << std::endl;
    8. std::cout << "double = " << sizeof( double ) << std::endl;
    9. std::cout << "long double = " << sizeof( long double ) << std::endl;
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 
    Result:
    $ ./a.out
    int = 4
    int * = 8
    float = 4
    double = 8
    long double = 16
    64 bits in 64 bit systems have nothing to do with floating point values, they don't have much to do with integers either --- they're needed to enlarge the address space.

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

    Default Re: system-independent C++ data types

    Quote Originally Posted by jacek View Post
    64 bits in 64 bit systems have nothing to do with floating point values, they don't have much to do with integers either --- they're needed to enlarge the address space.
    I don't agree. Not only the address bus is enlarged to 64 bits, but also the data bus, so it's not only about increasing the address space.

  19. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: system-independent C++ data types

    Quote Originally Posted by wysota View Post
    Not only the address bus is enlarged to 64 bits, but also the data bus, so it's not only about increasing the address space.
    I was referring to the programming model, nevertheless you can widen the data bus without changing the address length (but it depends whether you treat the cache and MMU as parts of a CPU or as external components).

Similar Threads

  1. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15
  2. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 15:36
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. Use QVariant with custon data types
    By mcosta in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 14:55

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.