views:

560

answers:

4

Hi there, does anybody know a website or a paper where the sizes of C data types were compared on different machines? I'm interested in values of some 'big' machines like a System z or the like.

And: Is there an upper bound of bytes that the biggest native datatype on any machine can have and is it always of the type complex long double?

Edit: I'm not sure about but does the SIMD register data also take advantage of the CPU's cache? Data types that will be stored in a special unit and do not use the L1/L2/L cache are out of my interesst. Only the types {char, short, int, long, long long, float, double, long double, _Bool, void *} (and with _Complex) will be examined.

A: 

The tricky thing about native types is that some architectures and compilers may extend them. For example, most compilers targeting modern Intel hardware offer a __m128 datatype, which is the width of a SIMD register. AVX will have a 256-bit SIMD width, Larrabee 512bit, and each has a corresponding native type in the compiler.

Crashworks
+3  A: 

The C data type size does not depend on the machine platform. It depends on the compiler implementation. Two different compilers on the same hardware platform might implement basic types differently, resulting in completely different sizes.

You should also take into account the fact that such standard types as size_t are not guaranteed to be represented by user-accessible types like unsigned int. It is quite legal and possible that size_t might be implemented through an implementation-specific unsigned type of unknown size and range.

Also, theoretically (and pedantically) C language has no "biggest" type in terms of size. C language specification makes absolutely no guarantees about the relative sizes of the basic types. C language only makes guarantees about the relative ranges of the representable values of each type. For example, the language guarantees that the range of int is not smaller than the range of short. However, since [almost] any type can contain an arbitrary amount of padding bits, theoretically the object size of type short might be greater than that of type int. This would be, of course, a very exotic situation.

In practice though, you can expect that long long int is the biggest integral type and long double is the biggest floating point type. You can also include the complex types into the consideration, if you wish to do so.

AndreyT
Pedant: char <= short <= long <= long long (C99)int can be unrelated to any of these. So it does make a guarantee about relative sizes, but not absolute sizes.
Joel
@Joel: Not true. C language standard says absolutely nothing about relative *sizes* of the types. Only about *ranges*. The relative *sizes* are mandated by C++ standard, but not by C standard.
AndreyT
I'm not sure what you might mean that the type of `size_t` might not be user-accessible. If nothing else, a user has to be able to declare variables of type `size_t`, and `size_t x = sizeof(size_t);` has to put something sensible into `x`.
Michael Burr
@Michael Burr: I meant that the type might be implementation-specific non-standard type, like some `_szt_128_int_t`. It might be "user-accessible" of course, but not directly "useable" in a sense that it is not portable by itself.
AndreyT
@AndreyT: got it.
Michael Burr
A: 

The long double type on x86 machines is 80 bits (10 bytes), http://en.wikipedia.org/wiki/Long_double, although this is an more-or-less obsolete form now. x64 doesn't support it.

John Knoeller
"Machines" know nothing about C's `long double` though. And nothing prevents a C implementation from implementing `long double` as a 8-byte FP type.
AndreyT
@AntryT: The question isn't about the C standard, but about variations in implementation. an 80 bit type _is_ supported by some x86 compilers.
John Knoeller
+1  A: 

You mention "native" datatypes, but note that complex is not defined by the C spec and thus isn't a native type. The native types for C are char,int,float, double, and void.

The size of a data type is generally determined by the underlying platform as well as the compiler. The C standard defines the minimum range for these types and defines a few relative relationships (long int must be at least as long as a regular int, etc). There's no easy way to determine the absolute size of any type without testing it.

When working with a new platform and I don't know the particular data type sizes, I write up a short app that dumps the result of sizeof for all the standard C types. There are also headers like stdint.h that give you data types that you can trust to be a certain size.

There is no upper bound as to the size of a data type. The C standard defines char to be "large enough to store any member of the execution character set". This partially binds the size of the native types to the machine architecture, and so a theoretical machine that had execution characters up to 1MB in size would have sizeof(char) equal to 1MB. Practically speaking, you probably won't find a machine where this is the case.

bta