changing an incoming function variable when calculating an internal variable. lazarus / 62-General ---------------------------------------------------------------------- Seenkao · 2021-09-25 22:18 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 memset(dstpp: Pointer; c: byte; len: SizeInt);var  i, cccc: LongWord;  dstp: PBYTE;  dstpLW: PLongWord;begin  if len = 0 then    exit;  i := 0;  dstp := dstpp;  if (len and 1) > 0 then  begin    dstp[i] := c;  inc(i);  end;  len := len shr 1;  if len = 0 then    exit;  if (len and 1) > 0 then  begin    dstp[i] := c;    dstp[i + 1] := c;    inc(i, 2);  end;  len := len shr 1;  if len = 0 then    exit;  dstpLW := dstpp;  cccc := c + c * $FF + c * $FF00 + c * $FF0000 + c * $FF000000;  // <<<<---- !!!!!  WriteLn(cccc);  if (len and 1) > 0 then  begin    dstpLW[i] := cccc;    inc(i, 4);  end;  len := len shr 1;  if len = 0 then    exit;  writeln(len);  while len > 0 do  begin    dstpLW[i] := cccc;    dstpLW[i + 4] := cccc;    dstpLW[i + 8] := cccc;    dstpLW[i + 12] := cccc;    dstpLW[i + 16] := cccc;    dstpLW[i + 20] := cccc;  dstpLW[i + 24] := cccc;    dstpLW[i + 28] := cccc;    inc(i, 32);    dec(len);  end;end;            in the line: cccc := c + c * $FF + c * $FF00 + c * $FF0000 + c * $FF000000; the variable "C" is reset to zero! Please check it out! FPC 3.2.0, Windows 7. Forgot. Comment out the code located after the specified line. There is an error in the code, there will be a crash. ---------------------------------------------------------------------- Zvoni · 2021-09-27 07:11 At a guess: You're calling the function with '#0' or '0' as the second parameter. something multiplied with zero returns zero, zero plus zero is still zero ---------------------------------------------------------------------- PascalDragon · 2021-09-27 07:11 Do you pass in the pointer to some local variable for dstpp? In that case it can be that you overwrite the stack and thus also where the compiler stores the c variable. And what are you trying to achieve with the c + c * $FF + c * $FF00 + c * $FF0000 + c * $FF000000 anyway? If you want to duplicate c into all four bytes of a 32-bit value then this is the wrong approach. ---------------------------------------------------------------------- Thaddy · 2021-09-27 07:36 You are overindexing by one. Try to subtract 1: 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   cccc := -1+c + c * $FF + c * $FF00 + c * $FF0000 + c * $FF000000;  // <<<< note the -1 at the startFull: 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 {$MODE objfpc}{$RANGECHECKS ON}procedure memset(dstpp: Pointer; c: byte; len: SizeInt);var  i, cccc: LongWord;  dstp: PBYTE;  dstpLW: PLongWord;begin  if len = 0 then    exit;  i := 0;  dstp := dstpp;  if (len and 1) > 0 then  begin    dstp[i] := c;    inc(i);  end;  len := len shr 1;  if len = 0 then    exit;  if (len and 1) > 0 then  begin  dstp[i] := c;    dstp[i + 1] := c;    inc(i, 2);  end;  len := len shr 1;  if len = 0 then    exit;  dstpLW := dstpp;  cccc := -1+c + c * $FF + c * $FF00 + c * $FF0000 + c * $FF000000;  // <<<<---- !!!!!  WriteLn(cccc);  if (len and 1) > 0 then  begin    dstpLW[i] := cccc;  inc(i, 4);  end;  len := len shr 1;  if len = 0 then    exit;  writeln(len);  while len > 0 do  begin    dstpLW[i] := cccc;    dstpLW[i + 4] := cccc;    dstpLW[i + 8] := cccc;    dstpLW[i + 12] := cccc;    dstpLW[i + 16] := cccc;    dstpLW[i + 20] := cccc;    dstpLW[i + 24] := cccc;    dstpLW[i + 28] := cccc;    inc(i, 32);    dec(len);  end;end;  var  D:Pointer;begin  D := AllocMem(1024);  MemSet(D,1,1024);end.Note that code looks a bit 32 bit centric... I also do not see the point of that code? Note the range check will reveal what was wrong. (201) ---------------------------------------------------------------------- Zvoni · 2021-09-27 08:42 Quote from: Thaddy on September 27, 2021, 09:36:49 amI also do not see the point of that code? Probably has to do with this: https://forum.lazarus.freepascal.org/index.php/topic,56304.0.html ---------------------------------------------------------------------- Seenkao · 2021-09-27 13:39 PascalDragon, я тестирую код. Это произошло во время тестов. Вполне возможно это не полноценная проверка, возможно я многого не учёл. Вероятно, если бы я использовал переменную "c" в дальнейшем, то я не получил бы её изменения. В данное время я точно знаю, как быстро дублировать один байт в 4 байта (8 или любое другое количество). Yandex translate: Pascal Dragon, I'm testing the code. This happened during the tests. It is quite possible that this is not a full-fledged check, perhaps I did not take into account a lot. Probably, if I had used the variable "c" in the future, I would not have received its changes. At this time, I know exactly how to quickly duplicate one byte into 4 bytes (8 or any other number of bytes). 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 cccc := c * 1010101; P.S. Probably I sometimes make hasty conclusions! ---------------------------------------------------------------------- Zvoni · 2021-09-27 13:53 Yes, as long as you DON'T call your memset function with something like "memset(SomePointer, 0, SomeSize)" in your first post in line 29 (where you assign cccc with your formula): zero multiplied with something is zero, zero plus zero is zero ---------------------------------------------------------------------- alpine · 2021-09-27 14:53 @Seenkao, cccc := c * $1010101; ---------------------------------------------------------------------- Seenkao · 2021-09-27 14:59 Quote from: Zvoni on September 27, 2021, 03:53:35 pmYes, as long as you DON'T call your memset function with something like "memset(SomePointer, 0, SomeSize)" in your first post in line 29 (where you assign cccc with your formula): zero multiplied with something is zero, zero plus zero is zero Нет! Это явно показывает, что вы не пытались даже вникнуть про что я пишу. В момент вычисления "cccc", "c" сбрасывается в нуль! Даже если входящее значение "c" равно 67 или любому другому числу (не нуль). Я не учёл в этом моменте, что в дальнейшем "c" не используется. Я просматривал ассемблерный код. И я перепутал паскаль с ассемблером. Компилятор увидел, что "c" больше не используется, и использовал регистр на своё усмотрение, не сохраняя значение "c". Yandex translate: No! This clearly shows that you have not even tried to understand what I am writing about. At the moment of calculating "cccc", "c" is reset to zero! Even if the incoming value of "c" is 67 or any other number (not zero). I did not take into account at this point that in the future "c" is not used. I was looking through the assembly code. And I confused pascal with assembler. The compiler saw that "c" was no longer used, and used the register at its discretion, without saving the value of "c". Quote from: y.ivanov on September 27, 2021, 04:53:12 pm@Seenkao, cccc := c * $1010101; Я выше это уже написал.  I have already written this above. ---------------------------------------------------------------------- alpine · 2021-09-27 15:18 Quote from: Seenkao on September 27, 2021, 04:59:15 pmQuote from: y.ivanov on September 27, 2021, 04:53:12 pm@Seenkao, cccc := c * $1010101; Я выше это уже написал.  I have already written this above. Pls, take note of the added red dollar sign. ---------------------------------------------------------------------- Seenkao · 2021-09-27 15:34 Yes, sometimes I'm in a hurry...