Common data types: how many bytes are they?

I would like to know how many bytes a

  • 32-bit integer

  • ASCII character (char in C ++?)

  • Pointer (4 bytes?)

  • Short

  • Float

Engaged in Delphi, and if it generally matches most languages

Also, do the above data types have a constant size? I mean, do the integers 0, 4, 123 and 32231 have the same size?

+4
source share
4 answers

A 32-bit integer is ALL four bytes because 1 byte = 8 bits.

  • An Integer is a 32-bit signed integer, and Cardinal is an unsigned 32-bit integer. Thus, they always occupy four bytes, regardless of the value that they represent. (In fact, it is extremely important that simple types have a fixed width — in fact, it depends on low-level programming! This is even the cornerstone of how computers work.)

  • The smaller integer types are Smallint (16-bit signature), Word (16-bit unsigned) and Byte (8-bit unsigned). The large integer types are Int64 (64-bit signature) and UInt64 (64-bit unsigned).

  • Char was 1-byte AnsiChar until Delphi 2009; now it's a 2-byte WideChar .

  • Pointer always 4 bytes because Delphi currently only creates 32-bit applications. When it supports 64-bit applications, Pointer will become 8 bytes.

  • There are three common types of floating point in Delphi. These are Single , Double (= Real ) and Extended . They occupy 4, 8 and 10 bytes, respectively.

To examine the size of this type, for example, Short , just try

 ShowMessage(IntToStr(SizeOf(Short))) 

Link:

+8
source

In C / C ++, SizeOf (Char) = 1 byte, as required by the C / C ++ standard.

In Delphi, the SizeOf (Char) parameter is version dependent (1 byte for versions other than Unicode, 2 bytes for Unicode versions), so Char in Delphi is more like TChar in C ++.

+2
source

This may be different for different machines, so you can use the following code to determine the size of the integer (for example):
cout << "Integer size:" << sizeof(int);

+1
source

I do not want to confuse you too much, but there is a problem of alignment; If you define such an entry, it will depend on the compiler, what its layout will look like:

 type Test = record A: Byte; B: Pointer; end; 

If compiled with {$ A1}, SizeOf(Test) will end as 5, while compiling with {$ A4} will give you 8 (at least on the current 32-bit Delphi, which is there!) There are all kinds of small tricks here, so I would advise now to ignore this and read an article like this when the need arises; -)

0
source

All Articles