FPC 3.2.2 using Result with PChar - how to ? lazarus / 21-General ---------------------------------------------------------------------- paule32 · 2025-04-18 12:57 Hello, currently, I doing voodoo programming ... I have the following Code below, and I get different results for each call. When I comment out MessageBoxA in StrCopy_ then all okay - but with message. When I comment in MessageBoxA in StrCopy_ then I get no message Box but the result will be empty. What is wrong there ? 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 StrCopy_(var Dest: PChar; Source: PChar): PChar; stdcall; export;var  I : Integer;  L : Integer;begin  L      := StrLen(Source);    Dest   := StrAlloc(L + 1);  result := StrAlloc(L + 1);    i := 0;  repeat    if Source[i] = #0 then    break;    result[i] := Source[i];    Dest  [i] := Source[i];    inc(i);  until i = L;    Dest  [L] := #0;  result[L] := #0;    // when the below function is comment in, then  // I get empty string in StrCopy Function  // is it comment out like now, I get the same string in StrCopy as there.  MessageBoxA(0,Dest,'OPOPOPOP',0);end;function StrCopy(var Dest: PChar; Source: PChar): PChar; stdcall;var  D: PChar;  S: PChar;begin  result := StrAlloc(StrLen(Source) + 1);  StrCopy_(Dest, Source);   // there will Dest empty, if MessageBoxA from above call is empty  MessageBoxA(0,Dest,'OPOP  OPOP',0);  result := Dest;end; ---------------------------------------------------------------------- paule32 · 2025-04-18 16:13 I had done it. The FP Compiler optimize very fast and devlish good. So, I have to explicit use the Exit(D); Function to force "touch the Result". Else, the Result will optimzed away... One next reason was the overlaping Result and Dest. 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 StrCat_(var Dest: PChar; Source: PChar): PChar; stdcall; export;var  D       : PChar;  L, I, J : Integer;begin  L := StrLen(Dest) + StrLen(Source) + 1;  D := StrAlloc(L);    for i := 0 to StrLen(Dest) - 1 do  D[i] := Dest[i];    for J := 0 to StrLen(Source) - 1 do  D[i + j + 1] := Source[J];  D[i + J + 2] := #0;    Dest := D;  Exit(D);end; ---------------------------------------------------------------------- jamie · 2025-04-18 17:56 of course it seems you think that memory grows on trees. Did you think about cleanup ? ---------------------------------------------------------------------- Remy Lebeau · 2025-04-18 18:07 Quote from: paule32 on April 18, 2025, 02:57:24 pmI have the following Code below, and I get different results for each call. Why are you allocating dynamic memory twice?  You are leaking memory. For that matter, a StrCopy() implementation should just copy data into an existing buffer that the caller supplies in the Dest parameter, and then return the same Dest pointer that was passed in, eg: 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 StrCopy_(Dest, Source: PChar): PChar; stdcall; export;var  I, L : Integer;begin  L := StrLen(Source);    i := 0;  repeat    if Source[i] = #0 then      break;    Dest[i] := Source[i];    Inc(i);  until i = L;    { alternatively, and faster since StrLen() is redundant:  i := 0;  while Source[i] <> #0 do  begin    Dest[i] := Source[i];    Inc(i);  end;    }    Dest[i] := #0;  Result := Dest;end; function StrCopy(Dest: PChar; Source: PChar): PChar; stdcall;begin  Result := StrCopy_(Dest, Source);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 var buffer: array[0..10] of Char;StrCopy(buffer, 'Hello'); ---------------------------------------------------------------------- n7800 · 2025-04-22 18:26 Quote from: paule32 on April 18, 2025, 06:13:32 pmCode: 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 StrCat_(var Dest: PChar; Source: PChar): PChar; stdcall; export;var  D       : PChar;  L, I, J : Integer;begin  L := StrLen(Dest) + StrLen(Source) + 1;  D := StrAlloc(L);    for i := 0 to StrLen(Dest) - 1 do  D[i] := Dest[i];    for J := 0 to StrLen(Source) - 1 do  D[i + j + 1] := Source[J];  D[i + J + 2] := #0;    Dest := D;  Exit(D);end;  @paule32, note that in this code you use (on lines 13-14) the variable "i" after the loop (on lines 9-10). It's not obvious, but it's wrong, because it doesn't mean it will stay equal to the last index. See the documentation or the big thread about it. In such cases you have to use "while" or something else. ---------------------------------------------------------------------- silvercoder70 · 2025-04-22 22:14 If you are trying to mimic what C does then goes with what Remy says. Even with strcat (or StrCat) the memory in the destination is expected to be large enough. ---------------------------------------------------------------------- paule32 · 2025-04-23 04:46 Thanks for yours replies. The Point is, that I have to use "Exit()". Because FPC is very good in Optimize Code. And sometimes the Compiler is tending to Cut out relevant parts. With a explicit "Exit", the Compiler is knowing a Function Call (or it thinks they have to create Code to call a Function). That is what I have learned with FPC. I was thinking over, that the Computer and in this case there the Compiler does what the Developer would like do - but the Counter Part is the case. Okay, not all in the box can have the Abracadaberro Master ;-) ---------------------------------------------------------------------- PascalDragon · 2025-04-23 18:54 Quote from: paule32 on April 23, 2025, 06:46:42 amBecause FPC is very good in Optimize Code. And sometimes the Compiler is tending to Cut out relevant parts. If the compiler is cutting stuff that you assume to be relevant then it's more often then not a bug in your code. Quote from: paule32 on April 23, 2025, 06:46:42 amWith a explicit "Exit", the Compiler is knowing a Function Call (or it thinks they have to create Code to call a Function). How did the code look like before you used Exit()? ---------------------------------------------------------------------- paule32 · 2025-04-23 21:16 result := ... ---------------------------------------------------------------------- Thaddy · 2025-04-24 06:06 If you want have your result as a PChar, you need to allocate the correct amount of heap memory for result. The error you make is having it only point to another value, not copy, inside the function. Every variable that is local will disappear. That value is stack memory and will disappear on function exit: BOOOOM. Guaranteed SIGSEV. A PChar is a pointer type, not a value type, hence the P. ---------------------------------------------------------------------- PascalDragon · 2025-04-26 13:41 Quote from: paule32 on April 23, 2025, 11:16:52 pmresult := ... Assuming it's located right at the end of the function then there should be no difference between using Result and using Exit. If there is then something is really wrong with your code.