Sign in Join
Wiki copytemporary

SizeOf/de: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: SizeOf/de

OldNewLine
1+## SizeOf
2+
3+### Einleitung
4+
5+**SizeOf** liefert die Grösse einer Variable/Type zurück.
6+
7+Bei dynamischen Arrays und bei Strings ist zu beachten, da dort nur die Grösse des Zeigers geliefert wird.
8+
9+Bei **ShortString** ist dies anders, da wird die effektive Grösse des Stringes geliefert.
10+
11+### Beispiel
12+
13+``` pascal
14+type
15+ Tr = record
16+ a, b, c: byte;
17+ end;
18+
19+var
20+ b: byte;
21+ w: word;
22+
23+ sa: array[0..5] of byte;
24+ da: array of byte;
25+
26+ r: record
27+ b: byte;
28+ w: word;
29+ end;
30+
31+ s: string;
32+ ss: ShortString;
33+
34+begin
35+ WriteLn('Byte: ', SizeOf(b)); // Ausgabe: 1
36+ WriteLn('Word: ', SizeOf(w)); // Ausgabe: 2
37+ WriteLn('Statische Array: ', SizeOf(sa)); // Ausgabe: 6
38+
39+ SetLength(da, 13);
40+ WriteLn('Dynamische Array: ', SizeOf(da)); // Ausgabe: 8, bei 64Bit OS
41+
42+ WriteLn('Record: ', SizeOf(r)); // Ausgabe: 4
43+ WriteLn('Record Type: ', SizeOf(Tr)); // Ausgabe: 3
44+
45+ s := 'abcdef';
46+ WriteLn('String: ', SizeOf(s)); // Ausgabe: 8, bei 64Bit OS
47+ WriteLn('ShortString: ', SizeOf(ss)); // Ausgabe: 256
48+```
49+
50+### dynamische Array
51+
52+Da bei einer dynamischen Array muss die Grösse mit **Length** und **SizeOf** ermitteln.\
53+Auch bei einem **String** muss man sich mit Length behelfen, ausgenommen **ShortString**
54+
55+``` pascal
56+var
57+ da: array of Word;
58+begin
59+ SetLength(da, 13);
60+ WriteLn(Length(da)* SizeOf(Word)); // Ausgabe: 26
61+```
62+
63+## BitSizeOf
64+
65+**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;`
66+
67+``` pascal
68+var
69+ a: bitpacked array [0..9] of Boolean;
70+begin
71+ WriteLn(BitSizeOf(a)); // Ausgabe: 16
72+ WriteLn(BitSizeOf(a[5])); // Ausgabe: 1
73+ WriteLn(BitSizeOf(Byte)); // Ausgabe: 8
74+```
75+
76+Autor: Mathias
77+
78+---
79+*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](/wiki-contact).*
80+Category: Code/de
81+Category: Deutsch
82+Category: Pascal/de

Back to history