Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of Primitive data types

Tags:

c++

c

sizeof

On what exactly does the size of a primitive data type like int depend on?

  • Compiler
  • Processor
  • Development Environment

Or is it a combination of these or other factors?
An explanation on the reason of the same will be really helpful.

EDIT: Sorry for the confusion..I meant to ask about Primitive data type like int and not regarding PODs, I do understand PODs can include structure and with structure it is a whole different ball game with padding coming in to the picture. I have corrected the Q, the edit note here should ensure the answers regarding POD don't look irrelevant.

like image 852
Alok Save Avatar asked Dec 30 '10 11:12

Alok Save


People also ask

What are the 5 primitive data types?

Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.


3 Answers

I think there are two parts to this question:

  1. What sizes primitive types are allowed to be.
    This is specified by the C and C++ standards: the types have allowed minimum value ranges they must have, which implicitly places a lower bound on their size in bits (e.g. long must be at least 32 bit to comply with the standard).
    The standards do not specify the size in bytes, because the definition of the byte is up to the implementation, e.g. char is byte, but byte size (CHAR_BIT macro) may be 16 bit.

  2. The actual size as defined by the implementation.
    This, as other answers have already pointed out, is dependent on the implementation: the compiler. And the compiler implementation, in turn, is heavily influenced by the target architecture. So it's plausible to have two compilers running on the same OS and architecture, but having different size of int. The only assumption you can make is the one stated by the standard (given that the compiler implements it).
    There also may be additional ABI requirements (e.g. fixed size of enums).

like image 97
Alex B Avatar answered Oct 21 '22 03:10

Alex B


First of all, it depends on Compiler. Compiler in turns usually depends on the architecture, processor, development environment etc because it takes them into account. So you may say it's a combination of all. But I would NOT say that. I would say, Compiler, since on the same machine you may have different sizes of POD and built-in types, if you use different compilers. Also note that your source code is input to the compiler, so it's the compiler which makes final decision of the sizes of POD and built-in types. However, it's also true that this decision is influenced by the underlying architecture of the target machine. After all, the real useful compiler has to emit efficient code that eventually runs on the machine you target.

Compilers provides options too. Few of them might effect sizes also!


EDIT: What Standards say,


Size of char, signed char and unsigned char is defined by C++ Standard itself! Sizes of all other types are defined by the compiler.

C++03 Standard $5.3.3/1 says,

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [Note: in particular,sizeof(bool) and sizeof(wchar_t) are implementation-defined.69)

C99 Standard ($6.5.3.4) also itself defines the size of char, signed char and unsigned char to be 1, but leaves the size of other types to be defined by the compiler!


EDIT:

I found this C++ FAQ chapter really good. The entire chapter. It's very tiny chapter though. :-)

http://www.parashift.com/c++-faq-lite/intrinsic-types.html


Also read the comments below, there are some good arguments!

like image 39
17 revs Avatar answered Oct 21 '22 04:10

17 revs


If you're asking about the size of a primitive type like int, I'd say it depends on the factor you cited.

The compiler/environment couple (where environment often means OS) is surely a part of it, since the compiler can map the various "sensible" sizes on the builtin types in different ways for various reasons: for example, compilers on x86_64 Windows will usually have a 32 bit long and a 64 bit long long to avoid breaking code thought for plain x86; on x86_64 Linux, instead, long is usually 64 bit because it's a more "natural" choice and apps developed for Linux are generally more architecture-neutral (because Linux runs on a much greater variety of architectures).

The processor surely matters in the decision: int should be the "natural size" of the processor, usually the size of the general-purpose registers of the processor. This means that it's the type that will work faster on the current architecture. long instead is often thought as a type which trades performance for an extended range (this is rarely true on regular PCs, but on microcontrollers it's normal).

If in instead you're also talking about structs & co. (which, if they respect some rules, are POD), again the compiler and the processor influence their size, since they are made of builtin types and of the appropriate padding chosen by the compiler to achieve the best performance on the target architecture.

like image 43
Matteo Italia Avatar answered Oct 21 '22 03:10

Matteo Italia