SizeOf/de
Sent here from BitSizeOf/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
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: 256dynamische 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
var
da: array of Word;
begin
SetLength(da, 13);
WriteLn(Length(da)* SizeOf(Word)); // Ausgabe: 26BitSizeOf
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;
var
a: bitpacked array [0..9] of Boolean;
begin
WriteLn(BitSizeOf(a)); // Ausgabe: 16
WriteLn(BitSizeOf(a[5])); // Ausgabe: 1
WriteLn(BitSizeOf(Byte)); // Ausgabe: 8Autor: 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.
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
