Forum > FPC development

rtl/inc/objpas.inc procedure InitInterfacePointers observation

(1/2) > >>

lagprogramming:
Looking at procedure InitInterfacePointers(objclass: tclass;instance : pointer); I've noticed that variable "i" is of type longint and intftable^.EntryCount is of type sizeuint. I was thinking that maybe it would be better to make i of sizeuint, too.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---      procedure InitInterfacePointers(objclass: tclass;instance : pointer);         var          ovmt: PVmt;          i: longint;          intftable: pinterfacetable;          Res: pinterfaceentry;        begin          ovmt := PVmt(objclass);          while assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}assigned(ovmt^.vIntfTable){$endif} do            begin              intftable:=ovmt^.vIntfTable;              {$ifdef VER3_0}              if assigned(intftable) then              {$endif VER3_0}              begin                i:=intftable^.EntryCount;                Res:=@intftable^.Entries[0];                while i>0 do begin                  if Res^.IType = etStandard then                    ppointer(@(pbyte(instance)[Res^.IOffset]))^:=                      pointer(Res^.VTable);                  inc(Res);                  dec(i);                end;              end;              ovmt:=ovmt^.vParent;            end;        end;
Also, in the same file, immediately after the above procedure comes class function TObject.InitInstance(instance : pointer) : tobject; This function has variable "i" of type longint, too. Somebody might check the variable type because the function has the line "for i:=0 to mopinittable^.Count-1 do" and mopinittable^.Count is of type longword.

BrunoK:
Test this little program to see what happens if 'i' is defined as sizeuint.
One loop uses i : longint and the next one uses sui : sizeuint as indexes.
In case i is declared as longint, FPC takes appropriate steps to adjust types for the loop (probably casting to longint).

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program pgmLongVsSizeUI; var  i: longint;  sui : sizeuint;  c: sizeuint;  a: array [0..9] of pointer;begin  c := 0;  WriteLn('Using i');  for i := 0 to c - 1 do    writeln(i : 6, HexStr(a[i]):18);  Write('Press enter'); ReadLn;   WriteLn('Using sui');  for sui := 0 to c - 1 do begin    writeln(sui : 6, HexStr(a[sui]):18);    if sui>= 99 then      break;  end;  Write('Press enter'); ReadLn;end.

lagprogramming:
It's not mandatory to keep the for loop.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  WriteLn('Using sui');  for sui := 0 to c - 1 do begin    writeln(sui : 6, HexStr(a[sui]):18);    if sui>= 99 then      break;  end; 
  can be replaced with something like
 

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  WriteLn('Using sui');  if c>0 then  begin    sui:=0;    repeat      writeln(sui : 6, HexStr(a[sui]):18);      inc(sui);    until sui=c;  end;
Edit:
Or you can still keep the for loop just by adding "if c>0 then" before "for"


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  WriteLn('Using sui');  if c>0 then    for sui := 0 to c - 1 do begin      writeln(sui : 6, HexStr(a[sui]):18);    end;

BrunoK:

--- Quote from: lagprogramming on March 20, 2023, 04:13:51 pm ---It's not mandatory to keep the for loop.

--- End quote ---
So how would you rewrite procedure InitInterfacePointers(objclass: tclass;instance : pointer); using i : SizeUInt and be sure it works in every case ?

BrunoK:

--- Quote from: BrunoK on March 20, 2023, 04:25:08 pm ---
--- End quote ---
Edit above message  : I meant in procedure TObject.InitInstance(instance : pointer) : tobject; the for loop

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---                       for i:=0 to mopinittable^.Count-1 do                         TRTTIRecVarOp(mopinittable^.Entries[i].ManagmentOperator)(PByte(Instance)+mopinittable^.Entries[i].FieldOffset); 

Navigation

[0] Message Index

[#] Next page

Go to full version