Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common data types: How much bytes are they?

Tags:

memory

delphi

I'd like to know how much bytes a

  • 32-bit integer

  • ASCII character (char in C++?)

  • Pointer (4 bytes?)

  • Short

  • Float

Takes up in Delphi, and if it is generally the same in most languages

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

like image 266
Hal Avatar asked Sep 11 '10 16:09

Hal


3 Answers

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

  • An Integer is a signed 32-bit integer, and a Cardinal is a unsigned 32-bit integer. These thus always occupy four bytes, irrespective of the value they represent. (In fact, it is an extremely important fact that simple types do have fixed widths -- low-level programming really depends on this! It is even a cornerstone part of how computers work.)

  • Smaller integer types are Smallint (16-bit signed), Word (16-bit unsigned) and Byte (8-bit unsigned). Larger integer types are Int64 (64-bit signed) and UInt64 (64-bit unsigned).

  • Char was a 1-byte AnsiChar prior to Delphi 2009; now it is a 2-byte WideChar.

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

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

To investigate the size of a given type, e.g. Short, simply try

ShowMessage(IntToStr(SizeOf(Short)))

Reference:

  • http://docwiki.embarcadero.com/RADStudio/en/Simple_Types
like image 174
Andreas Rejbrand Avatar answered Nov 12 '22 16:11

Andreas Rejbrand


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

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

like image 44
kludg Avatar answered Nov 12 '22 14:11

kludg


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

like image 28
vasin Avatar answered Nov 12 '22 14:11

vasin