[Interest] "QDataStream::version()" -- safe to assume 8-bit?

Charley Bay charleyb123 at gmail.com
Sun Oct 27 01:44:11 CEST 2013

The QDataStream docs give examples on serializing for compatible
formats across versions defined by QDataStream:


Other examples on the web are similar, like:


The recommendation is something like:

  QDataStream out(&file);

  // Write a header with a "magic number" and a version
  out << (quint32)0xA0B0C0D0;
  out << (qint32)123;

Then, reading in is:

  QDataStream in(&file);

  // Read and check the header
  quint32 magic;
  in >> magic;
  if (magic != 0xA0B0C0D0)

  // Read the version
  qint32 version;
  in >> version;

QUESTION #1:  Is it safe to write the 32-bit magic-number before
reading-and-setting the "QDataStream::version()" ?  It seems like
these examples are not cross-platform, as the 32-bit value may have
varying formats on different platforms.

Other reading suggests that it is "safe" to assume
"QDataStream::version()" wholly fits into an 8-bit value, so this
should not be a problem with that value.

QUESTION #2:  Is it forever safe to assume an 8-bit value is
sufficient to store "QDataStream::version()"?

Because of my concern regarding "#1", it seems like four
"magic-quint8" values should be serialized instead of one
"magic-quint32" value.

Or, am I just paranoid?


P.S.:   I'll close with the quote from the Digia page above because I
found it funny:

"We have two choices, either to attack I/O now and get it over with,
or to postpone I/O until near the end.

Neither prospect is very attractive."
  -- Donald E. Knuth

More information about the Interest mailing list