SizeOf: Difference between revisions

From Free Pascal wiki
Jump to navigationJump to search
(→‎usage: justification)
m (example code tabs -> spaces, Delphi code style)
Line 7: Line 7:
<syntaxhighlight lang="pascal" enclose="none">sizeOf</syntaxhighlight> is especially encountered in [[Assembler|assembly language]] or when doing manual allocation of memory:
<syntaxhighlight lang="pascal" enclose="none">sizeOf</syntaxhighlight> is especially encountered in [[Assembler|assembly language]] or when doing manual allocation of memory:
<syntaxhighlight lang="pascal" line highlight="20,31">
<syntaxhighlight lang="pascal" line highlight="20,31">
program sizeOfDemo(input, output, stderr);
program SizeOfDemo(input, output, stderr);


{$typedAddress on}
{$typedAddress on}


uses
uses
heaptrc;
  heaptrc;


type
type
s = record
  s = record
c: char;
    c: char;
i: longint;
    i: longint;
end;
  end;


var
var
x: ^s;
  x: ^s;


begin
begin
returnNilIfGrowHeapFails := true;
  ReturnNilIfGrowHeapFails := true;
getMem(x, sizeOf(x));
  GetMem(x, SizeOf(x));
if not assigned(x) then
  if not Assigned(x) then
begin
  begin
writeLn(stderr, 'malloc for x failed');
    WriteLn(stderr, 'malloc for x failed');
halt(1);
    Halt(1);
end;
  end;
x^.c := 'r';
  x^.c := 'r';
x^.i := -42;
  x^.i := -42;
freeMem(x, sizeOf(x));
  FreeMem(x, SizeOf(x));
end.
end.
</syntaxhighlight>
</syntaxhighlight>

Revision as of 11:45, 14 December 2018

Template:Translate The compile-time function sizeOf evaluates to the size in Bytes of a given data type name or variable identifier.

sizeOf can appear in compile-time expressions, inside compiler directives, too.

usage

sizeOf is especially encountered in assembly language or when doing manual allocation of memory:

program SizeOfDemo(input, output, stderr);

{$typedAddress on}

uses
  heaptrc;

type
  s = record
    c: char;
    i: longint;
  end;

var
  x: ^s;

begin
  ReturnNilIfGrowHeapFails := true;
	
  GetMem(x, SizeOf(x));
	
  if not Assigned(x) then
  begin
    WriteLn(stderr, 'malloc for x failed');
    Halt(1);
  end;
	
  x^.c := 'r';
  x^.i := -42;
	
  FreeMem(x, SizeOf(x));
end.

Direct handling of structured data types in assembly language requires awareness of data sizes, too:

program sizeOfDemo(input, output, stderr);

type
	integerArray = array of integer;

function sum(const f: integerArray): int64;
{$ifdef CPUx86_64}
assembler;
{$asmMode intel}
asm
	// ensure f is in a particular register
	mov rsi, f                       // rsi := f  (pointer to an array)
	
	// check for nil pointer (i.e. empty array)
	test rsi, rsi                    // rsi = 0 ?
	jz @sum_abort                    // if rsi = nil then goto abort
	
	// load last index of array [theoretically there is highF]
	mov rcx, [rsi] - sizeOf(sizeInt) // rcx := (rsi - sizeOf(sizeInt))^
	
	// load first element, since loop condition won't reach it
	{$if sizeOf(integer) = 4}
	mov eax, [rsi]                   // eax := rsi^
	{$elseif sizeOf(integer) = 2}
	mov ax, [rsi]                    // ax := rsi^
	{$else} {$error unexpected integer size} {$endif}
	
	// we're done, if f doesn't contain any more elements
	test rcx, rcx                    // rcx = 0 ?
	jz @sum_done                     // if high(f) = 0 then goto done
	
@sum_iterate:
	{$if sizeOf(integer) = 4}
	mov edx, [rsi + rcx * 4]         // edx := (rsi + 4 * rcx)^
	{$elseif sizeOf(integer) = 2}
	mov dx, [rsi + rcx * 2]          // dx := (rsi + 2 * rcx)^
	{$else} {$error unexpected scale factor} {$endif}
	
	add rax, rdx                     // rax := rax + rdx
	
	jo @sum_abort                    // if OF then goto abort
	
	loop @sum_iterate                // dec(rcx)
	                                 // if rcx <> 0 then goto iterate
	
	jmp @sum_done                    // goto done
	
@sum_abort:
	// load neutral element for addition
	xor rax, rax                     // rax := 0
	
@sum_done:
end;
{$else}
unimplemented;
begin
	sum := 0;
end;
{$endif}

begin
	writeLn(sum(integerArray.create(2, 5, 11, 17, 23)));
end.

With FPC the size of an integer depends on the used compiler mode. However, sizeOf(sizeInt) was inserted for demonstration purposes only. In the {$ifdef CPUx86_64} branch sizeOf(sizeInt) is always 8.

comparative remarks

dynamic arrays and alike

Since dynamic arrays are realized as pointers to a block on the heap, sizeOf evaluates to the pointer's size. In order to determine the array's size – of its data – sizeOf has to be used in conjunction with the function length.

program dynamicArraySizeDemo(input, output, stderr);

uses
	sysUtils;

resourcestring
	enteredN = 'You''ve entered %0:d integers';
	totalData = 'occupying a total of %0:d Bytes.';

var
	f: array of longint;

begin
	setLength(f, 0);
	
	while not eof() do
	begin
		setLength(f, length(f) + 1);
		readLn(f[length(f)]);
	end;
	
	writeLn(format(enteredN, [length(f)]));
	writeLn(format(totalData, [length(f) * sizeOf(f[0])]));
end.

The approach is the same for ANSI strings (depending on the {$longstrings} compiler switch state possibly denoted by string, too). Do not forget that dynamic arrays have management data in front of the referenced payload block. So if you really want to know, how much memory has been reserved for one array, you would have to take the high (last index in array) and reference count fields into account, too.

classes

Classes as well are pointers. The class TObject provides the function instanceSize. It returns an object's size as it is determined by the class's type definition. Additional memory that's allocated by constructors or any method, is not taken into account. Note, that classes might contain dynamic arrays or ANSI strings, too.