SRL Archive - OSR Outdated Tutorials

Type Sizes


Dgby714

2011-05-09 22:56:41

I've noticed a lot of our beginner scriptwriters here haven't noticed that types have sizes or how to get these sizes.

I wrote a small example script to show sizes and what they mean for the people who just wanna get to it and not read, It can be found neer the end.


You can get the size in bytes for a type doing the following. SizeOf(V);
var
B: byte;
begin
WriteLn(SizeOf(B)); //1
end.

You can get the maximum of a unsigned variable doing the following. Pow(2, SizeOf(V) * 8) - 1;
var
B: byte;
begin
WriteLn(Pow(2, SizeOf(B) * 8) - 1); //255
end.

You can get the maximum of a signed variable doing the following. (Pow(2, SizeOf(V) * 8) - 2) / 2;
Note: Add 1 and negate for Minimum.
var
B: byte;
begin
WriteLn((Pow(2, SizeOf(B) * 8) - 2) / 2); //127
end.


Byte
Size: 1 byte
Range: 0..255

ShortInt
Size: 1 byte
Range: -128..127

Word
Size: 2 bytes
Range: 0..65535

SmallInt
Size: 2 bytes
Range: -32768..32767

Cardinal (AKA LongWord)
Size: 4 bytes
Range: 0..4294967295

Integer
Size: 4 bytes
Range: -2147483648..2147483647


This next ones are not that important but ill include them =)

Single
Size: 4 bytes
Range: 1.5x10**-45..3.4x10**38

Double
Size: 8 bytes
Range: 5.0x10**-324..1.7x10**308

Extended
Size: 10 bytes
Range: 3.6x10**-4951..1.1x10**4932


program _;

function PadC(s: string; maxl: integer): string;
begin
Result := s;
while (Length(Result) < maxl) do
begin
Result := ' ' + Result + ' ';
if (Length(Result) = maxl - 1) then
Result := ' ' + Result;
end;
end;

function DebugType(Name: string; Size: integer; Unsigned: boolean): string;
var
MaxS: Extended;
begin
Result := '|' + PadC(Name, 12) + '|';

if (Size > 1) then
Result := Result + PadC(IntToStr(Size) + ' bytes', 10) + '|'
else
Result := Result + PadC(IntToStr(Size) + ' byte', 10) + '|';

MaxS := Pow(2, Size * 8) - 1;
if (not (Unsigned)) then
begin
MaxS := (MaxS - 1) / 2;
Result := Result + PadC('-' + FloatToStr(MaxS + 1) + '..' + FloatToStr(MaxS), 27) + '|';
end else
Result := Result + PadC('0..' + FloatToStr(MaxS), 27) + '|';
end;

var
B: Byte;
SI: ShortInt;
W: Word;
SmI: SmallInt;
C: Cardinal;
I: Integer;

begin
WriteLn('| Type | Size | Range |');
WriteLn(DebugType('Byte', SizeOf(B), True));
WriteLn(DebugType('ShortInt', SizeOf(SI), False));
WriteLn(DebugType('Word', SizeOf(W), True));
WriteLn(DebugType('SmallInt', SizeOf(SmI), False));
WriteLn(DebugType('Cardinal', SizeOf(C), True));
WriteLn(DebugType('Integer', SizeOf(I), False));
end.

Small Note: The script uses to function that asks for "Unsigned", first if a number is signed, 1 bit is taken and the number can be negative or positive. If its Unsigned the number goes from 0 to its maximum. As you can see above Byte and ShortInt have the same Size but diffrent Ranges, this is because Byte is unsigned.

HarryJames

2011-05-09 23:03:41

This is very helpful actually.
Would you add in the sizes for integer, string (I'm sure there gonna be a character limit)

Is char even supported?

Dgby714

2011-05-09 23:09:30

From: HarryJames
This is very helpful actually.
Would you add in the sizes for integer, string (I'm sure there gonna be a character limit)

Is char even supported?


Char = ShortInt;
Integer is already there.
String is basically an array of char so its only limited by memory.

HarryJames

2011-05-09 23:34:16

Thanks for char/string.
I didn't see integer - I'm on my iPod and very tired. I've got a full day of sixthform to start in an hour :(

Dgby714

2011-05-09 23:41:08

From: HarryJames
Thanks for char/string.
I didn't see integer - I'm on my iPod and very tired. I've got a full day of sixthform to start in an hour :(


It's there, but here,

Integer
Size: 4 bytes
Range: -2147483648..2147483647

HyperSecret

2011-05-10 00:26:21

I never knew we even had sizeof(), not that I would have ever used it…interesting though.

i luffs yeww

2011-05-10 00:48:27

;) I assume you were inspired by C? :)

Also, I had no idea PascalScript had a SizeOf operator. I assume it's actually a function and not a unary operator like in C though.

Reminds me that there's not a whole lot of information about why things work, rather than how to make them work. Makes me sad. :/

Rep++.

Dgby714

2011-05-10 00:51:06

From: i luffs yeww
;) I assume you were inspired by C? :)

Also, I had no idea PascalScript had a SizeOf operator. I assume it's actually a function and not a unary operator like in C though.

Reminds me that there's not a whole lot of information about why things work, rather than how to make them work. Makes me sad. :/

Rep++.


Noticed it when doing some C->Pascal work and playing with the Windows API in Simba =)

I've known about SizeOf for a long time tho I never knew what unsigned meant until learning C.

i luffs yeww

2011-05-10 01:18:16

C -> Pascal? Windows API? D: For what/why?

Anyway, does Pascal have unsigned types? More importantly, does PascalScript?

Dgby714

2011-05-10 01:22:23

From: i luffs yeww
C -> Pascal? Windows API? D: For what/why?

Anyway, does Pascal have unsigned types? More importantly, does PascalScript?


Yes but you cant like specify var name: unsigned integer; there named differently like a Cardinal is a unsigned integer.

BTW been playing with the Windows API can can now move the mouse and get mouse position in C. (Tho only in windows)

i luffs yeww

2011-05-10 01:33:31

All you have to do is #include <windows.h> isn't it? :p

Anyway, not on the right thread for this discussion imo.

Gu3st...

2011-05-10 05:22:30

SizeOf() Also works on custom types

type
TItem = record
Name : string;
StatsName : string;
UpText : TStringArray;
InvSlot : Integer;
Stackable : Boolean;
EquipLevel: Integer;
DTM : Integer;
end;

var
B : TItem;

begin
WriteLn(SizeOf(B));
end.

i luffs yeww

2011-05-10 09:46:35

From: Zyt3x
SizeOf() Also works on custom types


Well that makes sense, since custom types are just normal types, really. What would the size of that be, though? Not sure what size uninitialized strings and TStringArrays would be, so I really have no idea. I was guessing 16 (which would make strings and TStringArrays one byte?), but I don't know.

Echo_

2011-05-11 22:59:25

Quite an interesting read, thanks