tags:

views:

274

answers:

4

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?

+1  A: 

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);

vasin
he asked about delphi so cout won't work:P
flownt
It was C++ a minute ago :)
vasin
+7  A: 

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:

Andreas Rejbrand
Well, strictly speaking D1 is also Delphi :-)
Marco van de Voort
On 64-bit, a fourth 128-bit float type will probably be added, and the 10 byte one might disappear (the x87 status in 64-bit mode is a bit dubious, but it is allowed)
Marco van de Voort
+2  A: 

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++.

Serg
Except that in C++, a single project can be compiled as either Ansi or Unicode with a simple compiler directive. That is not the case in Delphi. D2009 and later can only compile as Unicode. Earlier versions can only compile as Ansi.
Remy Lebeau - TeamB
Erm, not true if we extend the question to the usage of custom string/character types, defined with an #ifdef on the compiler version. In that case, D2009+ could still compile to Ansi, while older versions (that support WideChar) could support (some sort of Unicode). (I'm ignoring the WinAPI/VCL/RTL integration issues here of course.)
PatrickvL
A: 

I don't want to confuse you too much, but there's also an alignment issue; If you define a record like this, it will depend on the compiler how it's layout will turn out :

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

If compiled with {$A1}, SizeOf(Test) will end up as 5, while compiling it with {$A4} would give you 8 (at least, on current 32 bit Delphi's that is!) There are all sorts of little gotcha's here, so I'd advise to ignore this for now and read an article like this when the need arises ;-)

PatrickvL
If you want the `sizeof` a record to be the sum of the `sizeof`s of every member, then simply write `packed record` instead of `record`.
Andreas Rejbrand