Sign in Join
Wiki copytemporary

SizeOf/de

Deutsch · English · Suomi · Русский

SizeOf

Einleitung

SizeOf liefert die Grösse einer Variable/Type zurück.

Bei dynamischen Arrays und bei Strings ist zu beachten, da dort nur die Grösse des Zeigers geliefert wird.

Bei ShortString ist dies anders, da wird die effektive Grösse des Stringes geliefert.

Beispiel

pascal
type
  Tr = record
    a, b, c: byte;
  end;

var
  b: byte;
  w: word;

  sa: array[0..5] of byte;
  da: array of byte;

  r: record
    b: byte;
    w: word;
  end;

  s: string;
  ss: ShortString;

begin
  WriteLn('Byte: ', SizeOf(b));              // Ausgabe: 1
  WriteLn('Word: ', SizeOf(w));              // Ausgabe: 2
  WriteLn('Statische Array: ', SizeOf(sa));  // Ausgabe: 6

  SetLength(da, 13);
  WriteLn('Dynamische Array: ', SizeOf(da)); // Ausgabe: 8, bei 64Bit OS

  WriteLn('Record: ', SizeOf(r));            // Ausgabe: 4
  WriteLn('Record Type: ', SizeOf(Tr));      // Ausgabe: 3

  s := 'abcdef';
  WriteLn('String: ', SizeOf(s));            // Ausgabe: 8, bei 64Bit OS
  WriteLn('ShortString: ', SizeOf(ss));      // Ausgabe: 256

dynamische Array

Da bei einer dynamischen Array muss die Grösse mit Length und SizeOf ermitteln.
Auch bei einem String muss man sich mit Length behelfen, ausgenommen ShortString

pascal
var 
  da: array of Word;
begin
  SetLength(da, 13);
  WriteLn(Length(da)* SizeOf(Word)); // Ausgabe: 26

BitSizeOf

BitSizeOf liefert anstelle von Byte, Bit zurück, und ist somit meistens 8x grösser. Eine Ausnahme ist zB. bei einem einzelnen Element einer bitpacked array [...] of Boolean;

pascal
var
  a: bitpacked array [0..9] of Boolean;
begin
  WriteLn(BitSizeOf(a));    // Ausgabe: 16
  WriteLn(BitSizeOf(a[5])); // Ausgabe:  1
  WriteLn(BitSizeOf(Byte)); // Ausgabe:  8

Autor: Mathias


From the Free Pascal wiki, page "SizeOf/de", as it stood on 2020-01-19 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 2 people, 2018–2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
MathiasCreated page
MathiasBitSizeOf
Mathias
MathiasBitSizeOf
TrevFixed template loop; wiki markup; categorised page (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.