Zeromemory vs. Fillchar lazarus / 62-General ---------------------------------------------------------------------- Ten_Mile_Hike · 2025-07-03 22:08 Hello Everyone,   Can anyone tell me why there is a "ZeroMemory(Destination: Pointer; Length: NativeUInt)" operation when we   already have FillChar(Destination^, Length, 0)? When should I use one over the other, pros and cons etc....     TIA As Always... ---------------------------------------------------------------------- dsiders · 2025-07-03 22:13 Quote from: Ten_Mile_Hike on July 04, 2025, 12:08:19 amHello Everyone,   Can anyone tell me why there is a "ZeroMemory(Destination: Pointer; Length: NativeUInt)" operation when we   already have FillChar(Destination^, Length, 0)? When should I use one over the other, pros and cons etc....     TIA As Always... Did you not notice? ZeroMemory is declared only for Windows and that is is implemented as: Code: [Select]procedure ZeroMemory(Destination:PVOID; Length:DWORD); begin   FillChar(Destination^,Length,#0); end; ---------------------------------------------------------------------- Thaddy · 2025-07-04 05:11 Background is simply that ZeroMemory is a Windows API call. https://learn.microsoft.com/en-us/previous- versions/windows/desktop/legacy/aa366920(v=vs.85) Since it is a C macro, in FPC it is implemented as a function. On Windows it is marked legacy and now has as alternative SecureZeroMemory. In FreePascal we don't need that. because the only reason Microsoft introduced it, is to prevent overaggressive C(++) compilers to simply remove the call to zeromemory altogether. FPC can't be that aggressive yet  It won't skip function calls. So in FPC SecureZeroMemory would be the exact same as ZeroMemory. ---------------------------------------------------------------------- 440bx · 2025-07-04 06:06 Quote from: Thaddy on July 04, 2025, 07:11:35 amBackground is simply that ZeroMemory is a Windows API call. RtlZeroMemory is a Windows function.  ZeroMemory is a macro. Quote from: Thaddy on July 04, 2025, 07:11:35 amhttps://learn.microsoft.com/en-us/previous- versions/windows/desktop/legacy/aa366920(v=vs.85) As is rather usual, MS doesn't tell the whole story. RtlZeroMemory is a function implemented in NTDLL, it's (32bit) code is:Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i .text:77062890 ; Exported entry 1304. RtlZeroMemory.text:77062890.text:77062890 ; =============== S U B R O U T I N E =======================================.text:77062890.text:7 7062890.text:77062890 ; __stdcall RtlZeroMemory(x, x).text:77062890               public _RtlZeroMemory@8.text:77062890 _RtlZeroMemory@8 proc near              ; DATA XREF: .text:off_77020210↑o.text:77062890.text:77062890 arg_0           = dword ptr  4.text:77062890 arg_4           = dword ptr  8.text:77062890.text:77062890                 push    edi.text:77062891                 mov     edi, [esp+4+arg_0].text:77062895                 mov     ecx, [esp+4+arg_4].text:77062899                 xor     eax, eax.text:7706289B                 cld.text:7706289C                 mov     edx, ecx.text:7706289E                 and     edx, 3.text:770628A1                 shr     ecx, 2.text:770628A4               rep stosd.text:770628A6                 or      ecx, edx.text:770628A8                 jnz     short loc_770628AE.text:770628AA                 pop     edi.text:770628AB               retn    8.text:770628AE ; ------------------------------ ---------------------------------------------.text:770628AE.text:77062 8AE loc_770628AE:                           ; CODE XREF: RtlZeroMemory(x,x)+18↑j.text:770628AE                 rep stosb.text:770628B0                 pop     edi.text:770628B1               retn    8.text:770628B1 _RtlZeroMemory@8 endp One nice thing about it is that it tries to minimize the number of moves. Quote from: Thaddy on July 04, 2025, 07:11:35 amSince it is a C macro, in FPC it is implemented as a function. It often is a macro that is a synonym of an inline implementation of memset.  BTW, it is definitely not a C macro when programming in Assembler.  In assembler it resolves to the ntdll function. Quote from: Thaddy on July 04, 2025, 07:11:35 amOn Windows it is marked legacy and now has as alternative SecureZeroMemory. It is questionable that the Secure version is more secure.  In both cases, sizeof is going to be used.  The "security" comes from the occasionally undesirable behavior from the C compiler.  IOW, as genuine functions (not macros), they are both as secure or insecure however you want to look at it. Quote from: Thaddy on July 04, 2025, 07:11:35 amIn FreePascal we don't need that. It's very useful in FPC, in C and in assembler. FPC generates inline versions, e.g, character arrays and structures initialized with Default() but Default is simply too inconvenient to be used. The inline capability is nice but, it can definitely grow the size of the executable (not that anyone who's using the LCL would ever notice.) ---------------------------------------------------------------------- Ten_Mile_Hike · 2025-07-04 07:45 Zeromemory is a wrapper for fillchar... (I should have seen it) ---------------------------------------------------------------------- d2010 · 2025-07-09 15:37 Quote from: Ten_Mile_Hike on July 04, 2025, 12:08:19 amHello Everyone, C:Q1=How to initialize my records  with string? E.g you search ???    I need initialize with fillchar? Perhaps a combinatioan fillchar and directly zero? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Typ  NavTree=^TreeRec;  TreeRec = record              dxf5:integer;                      //rem:contine adresa curenta a variabile tr            prefix,midifix,sufix:integer;              typ: NodeType;            left, right: NavTree;              rl: double;              s:string;tip:_Ttyps;StrL,StrR:word;              lNode,RNode:NodeType;              ls,rs:string;???           end;  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function makeNode(t: NodeType; l, r: NavTree): NavTree;begin    result:=nil;  New(result); ???//rem: if (tr<>nil) then fillchar(tr^,ksizeof_treerec,0);fillchar(tr^,sizeof(tr^)-1,0);???    with result^ do       begin         typ := t;         dxf5:=getmypid[1].lo; //rem:dxf5:=integer(tr);         rl:=0.0;       s:='';         prefix:=0;         if (length(fmpstr.left)>0) and (pp_vars2scr_setq(prefix,fmpstr.left,nil))                 then  fmpstr.left:='';          sufix:=0;         if (length(fmpstr.rigt)>0) and (pp_vars2scr_setq(sufix,fmpstr.rigt,nil))                 then fmpstr.rigt:='';          midifix:=0;         if (length(fmpstr.midi)>0)  and (pp_vars2scr_setq(midifix,fmpstr.midi,nil))                then  fmpstr.midi:='';         f113();         left := l;         right := r;    end;end;   function makeConstNode(T:NodeType;v: double;a:str63;b:_Ttyps): Tree;  var    tr: Tree;  begin    New(tr);???        //rem: if (tr<>nil) then fillchar(tr^,ksizeof_treerec,0);     // I ASK here???    with tr^ do begin        typ := T; rl := v;s:=a;tip:=b;         if s='$C[..]' then Begin strL:=laVal.strL;                                 strR:=laVal.strR;                           End;    end;    makeConstNode := tr;  end; ---------------------------------------------------------------------- Thaddy · 2025-07-09 17:10 Quote from: d2010 on July 09, 2025, 05:37:57 pmQuote from: Ten_Mile_Hike on July 04, 2025, 12:08:19 amHello Everyone, C:Q1=How to initialize my records  with string? E.g you search ???    I need initialize with fillchar? Perhaps a combinatioan fillchar and directly zero? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Typ  NavTree=^TreeRec;  TreeRec = record              dxf5:integer;                      //rem:contine adresa curenta a variabile tr            prefix,midifix,sufix:integer;              typ: NodeType;            left, right: NavTree;              rl: double;              s:string;tip:_Ttyps;StrL,StrR:word;              lNode,RNode:NodeType;              ls,rs:string;???           end;  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function makeNode(t: NodeType; l, r: NavTree): NavTree;begin    result:=nil;  New(result); ???//rem: if (tr<>nil) then fillchar(tr^,ksizeof_treerec,0);fillchar(tr^,sizeof(tr^)-1,0);???    with result^ do       begin         typ := t;         dxf5:=getmypid[1].lo; //rem:dxf5:=integer(tr);         rl:=0.0;       s:='';         prefix:=0;         if (length(fmpstr.left)>0) and (pp_vars2scr_setq(prefix,fmpstr.left,nil))                 then  fmpstr.left:='';          sufix:=0;         if (length(fmpstr.rigt)>0) and (pp_vars2scr_setq(sufix,fmpstr.rigt,nil))                 then fmpstr.rigt:='';          midifix:=0;         if (length(fmpstr.midi)>0)  and (pp_vars2scr_setq(midifix,fmpstr.midi,nil))                then  fmpstr.midi:='';         f113();         left := l;         right := r;    end;end;   function makeConstNode(T:NodeType;v: double;a:str63;b:_Ttyps): Tree;  var    tr: Tree;  begin    New(tr);???        //rem: if (tr<>nil) then fillchar(tr^,ksizeof_treerec,0);     // I ASK here???    with tr^ do begin        typ := T; rl := v;s:=a;tip:=b;         if s='$C[..]' then Begin strL:=laVal.strL;                                 strR:=laVal.strR;                           End;    end;    makeConstNode := tr;  end;  Can you make that code generic? So it can fill with bytes, words, integers, qwords etc? ---------------------------------------------------------------------- ASerge · 2025-07-09 18:35 Quote from: d2010 on July 09, 2025, 05:37:57 pmC:Q1=How to initialize my records  with string? The same. The compiler implicitly initializes all strings in records with a zero (in New call). So FillChar won't make it worse. Why do you use the value "SizeOf(...) - 1" in FillChar? ---------------------------------------------------------------------- Thaddy · 2025-07-09 19:09 Instead of length()? ---------------------------------------------------------------------- TBMan · 2025-07-09 19:22 I would have used Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i sizeof(TreeRec)  If you passed a nil pointer things could get wonky. ---------------------------------------------------------------------- d2010 · 2025-07-10 07:36 Quote from: Thaddy on July 09, 2025, 09:09:22 pmInstead of length()?  I break my function MakeNode onto theee functions Code: [Select]   makeNode_idf("fillchar")    makeNode_id2(",,,,:=0000 or :='' ");    makeNode_idz("zeromemory") I execute boca100=001 and I go\t   the results..All thre function all same results (fillchar isEqualTicks  zeromemory isEqual  d2) Code: [Select].oNow_MakeIdNode.zer="fElapsed",Time="00" oNow_MakeIdNode.zer="zElapsed",Time="00" .oNow_MakeIdNode.zer="0Elapsed",Time="00" But, when I reset boca100=100 at ??? isinside For loop:="; that 100xfillchar is more better that 100xZeroMemory.. 100xfillchars got 124 100xZeroMemory got 186; Other information/s about  "MakeIdNode.zer="0" you found  in attched.zip Code: [Select]oBod!t086good..oNow_MakeIdNode.zer="fElapsed",Time="124" oBod!t086good..oNow_MakeIdNode.zer="zElapsed",Time="186" oBod!t086good..oNow_MakeIdNode.zer="0Elapsed",Time="79" The "bigger" is worst, the "small" is more better. I run my Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var boca100=001;???Function makeNode_id0(rtype: NodeType;lef,rig: NavTree): NavTree;Var dwg,loop:integer;Begin dwg:=GetTickCount;      New(result);      for loop:=01 to boca100 do       Case Now_MakeIdNode.zer of'f':  makeNode_idf(result,rtype,lef,rig);'0':  makeNode_id2(result,rtype,lef,rig);'z':  makeNode_idz(result,rtype,lef,rig);      End;      Now_MakeIdNode.tim:=Now_MakeIdNode.tim+(GetTickCount-dwg);End; ---------------------------------------------------------------------- ASerge · 2025-07-10 18:25 Quote from: d2010 on July 10, 2025, 09:36:14 amBut, when I reset boca100=100 at ??? isinside For loop:="; that 100xfillchar is more better that 100xZeroMemory.. 100xfillchars got 124 100xZeroMemory got 186; The functions differ in line 7: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure makeNode_idf(Var dest:NavTree;rtype: NodeType; lef, rig: NavTree);begin    fillchar(dest^,sizeof(TreeRec),0);    with dest^ do       begin         typ := rtype;         dxf5:=integer(dest);         rle:=0.0;...Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure makeNode_idz(Var dest:NavTree;rtype: NodeType; lef, rig: NavTree);begin    ZeroMemory(dest,sizeof(TreeRec));    with dest^ do       begin         typ := rtype;         dxf5:=getmypid[1].lo;        rle:=0.0;...