6

I remember reading somewhere that Qt guarantees the size of some data types on supported platforms. Is it that int will be at least 32 bits everywhere, and qint32 will be exactly 32 bits everywhere? Or something else?

C++ guarantees that int will be at least 16 bits, and some Qt structures like QRect and QPoint use int internally. I'm developing an application where 32 bits is needed with those types, and I don't want to have to duplicate their functionality so I can use a larger type.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Jake Petroules
  • 23,472
  • 35
  • 144
  • 225

2 Answers2

6

The size of an integer type is up to the compiler. I don't think there's a guarantee that plain int will be of a precise size. But you can make sure you know it's not what you want by adding this line to the beginning of your main():

if(sizeof(int) != 4) {
  throw std::runtime_error("int is not 32-bit");
}
Stephen Chu
  • 12,724
  • 1
  • 35
  • 46
  • Ok, but, `QtGlobal` defines several typedefs. For example, the documentation for qint32 says "Typedef for signed int. This type is guaranteed to be 32-bit on all platforms supported by Qt.". So this means that `int` must also be guaranteed to be 32 bits on all platforms supported? – Jake Petroules Jun 21 '10 at 19:01
  • I would not derive that conclusion. The doc guarantees that qint32 will be of 32-bit size on all supported platforms, not the other way around. If they have to support a platform in the future that int is not 32-bit, they will change the typedef for that platform to reflect that. As it's implemented now, you can assume all platforms supported by Qt have 32-bit int. But assumption is a VERY EVIL thing. – Stephen Chu Jun 21 '10 at 19:26
  • 1
    The purpose of qint32 and friends is to provide a type with a guaranteed signedness and size. If the standard types would provide that, one wouldn't need those Qt typedefs ;) I guess the Qt documentation is ambiguously worded here. – Frank Osterfeld Jun 22 '10 at 07:11
  • You can't know how many bits there are in `int` by checking its size with `sizeof`. See http://stackoverflow.com/q/271076/95735 – Piotr Dobrogost Jan 04 '11 at 21:50
  • @Piotr: are you going to test that on *each platform supported by Qt*? Good luck! – Roman L Jan 04 '11 at 21:53
0

While, as far as I know, it's technically possible that int isn't 32bits, I've never seen a platform where it isn't. Imagine- char, 8bits, short, 16bits, int, .. 24bits? It simply doesn't fit the hierarchy for int to be non-32bits.

In addition, you can use UINT_MAX to confirm int's size on your given compiler.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • There are tons of microcontrollers where int is 16 bit. – Falmarri Jan 04 '11 at 21:49
  • Size (obtained by using `sizeof` operator) and **minimal** number of bits (so that the value of `UINT_MAX` could be represented) are two different things. See http://stackoverflow.com/q/271076/95735 – Piotr Dobrogost Jan 04 '11 at 22:00