AV in operator overloading lazarus / 21-General ---------------------------------------------------------------------- fcu · 2025-05-27 07:31 Hi if you run this example you'll get AV ,obviously its accessing pointer before initializing it , but in this example i do initializing it , so i am a bit confused ! 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 program test;{$ifdef FPC}{$mode objfpc}{$h+}{$endif}{$ifdef mswindows}{$apptype console}{$endif}{$modeswitch advancedrecords}uses {$ifdef FPC}{$ifdef unix}cthreads,cwstring,{$endif}{$endif} sysutils;  type trec = record  public   data : pint32;  public   procedure initptr(v: int32);   procedure freeptr;  public   class operator  + (a,b: trec):trec; end;  procedure trec.initptr(v : int32); begin  new(data);  data^ := v; end;  procedure trec.freeptr; begin  dispose(data); end;  class operator trec.+ (a,b: trec):trec; begin    result.data^ :=  a.data^ + b.data^;     end;  var  a,b,c : trec;begin  a.initptr(10); b.initptr(10); c.initptr(0); // initililization  c := a + b; // AV c.data is not initialized !?     writeln(c.data^); a.freeptr; b.freeptr; c.freeptr;  readln; end. ---------------------------------------------------------------------- cdbc · 2025-05-27 07:38 Hi 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  class operator trec.+ (a,b: trec):trec; begin    result.data^ :=  a.data^ + b.data^;     end;Why doesn't the 'Result' variable deserve to be initialized?!? I dunno, maybe like this: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 class operator trec.+ (a,b: trec):trec; begin    result.initptr(a.data^ + b.data^);     end;And then: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 a,b,c : trec;begin  a.initptr(10); b.initptr(10); // initililization  c := a + b;  writeln(c.data^); a.freeptr; b.freeptr; c.freeptr; readln;end.Regards Benny ---------------------------------------------------------------------- Martin_fr · 2025-05-27 07:42 result works like an "out param". It is not passed in. So the result in your operator is not initialized, it is a new uninitialised local variable. And only when your operator returns, then it is assigned to c (overwriting the content of c). This is even true for "operator :=". This will assign (e.g. cast, or in whatever way create the data) to a local "result". And that local result is then copied over to the variable in the caller. And yes, that means there will be extra memory movement. ---------------------------------------------------------------------- cdbc · 2025-05-27 07:44 Hi Edited my previous post... Regards Benny ---------------------------------------------------------------------- cdbc · 2025-05-27 07:50 Hi Martin Would that mean my example is wrong, he doesn't override the := operator?!? edit: Just tested it with heaptrc and it works flawlessly in fpc 3.2.2 edit2: Here's my test program on linux: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 program plusoperator; {$mode objfpc}{$H+}{$modeswitch advancedrecords}uses cthreads,cwstring,sysutils;  type trec = record  public   data : pint32;  public   procedure initptr(v: int32);   procedure freeptr;  public   class operator  + (a,b: trec):trec; end;  procedure trec.initptr(v : int32); begin  new(data);  data^ := v; end;  procedure trec.freeptr; begin  dispose(data); end;  class operator trec.+ (a,b: trec):trec; begin    result.initptr(a.data^ + b.data^); end;  var a,b,c : trec;begin  a.initptr(10); b.initptr(10); // initialization  c := a + b;   writeln(c.data^); a.freeptr; b.freeptr; c.freeptr; readln;end. Output = 20, No mem-leaks Regards Benny ---------------------------------------------------------------------- Martin_fr · 2025-05-27 08:08 Quote from: cdbc on May 27, 2025, 09:50:02 amWould that mean my example is wrong, he doesn't override the := operator?!? Depends... Depends on what exactly you expect to be overridden. ":=" does an assignment. It also (matter of viewpoint) does a conversion if needed and if known how to. This will NOT poke 8 bytes into the byte value. 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 SomeByteVal := SomeQWord; The operator := does the conversion part, if you so chose to view it that way. And that expresses itself in the following 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 program Project1;{$Mode objfpc}type  TFoo = record end; operator := (a: TFoo): TFoo;beginend;beginend.returning Error: Impossible to overload assignment for equal types ---------------------------------------------------------------------- fcu · 2025-05-27 08:37 thanks , i thought it may works like c++ Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i struct sData{public:  sData(int v){ data = new int; *data = v;}  sData& operator  + (sData d){     *data += *d.data;    return *this;  }public:  int * data;}; ---------------------------------------------------------------------- Thaddy · 2025-05-27 10:52 It essentially works like C++ but you did the new part wrong. ---------------------------------------------------------------------- PascalDragon · 2025-05-27 20:07 Quote from: fcu on May 27, 2025, 10:37:03 amthanks , i thought it may works like c++ Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i struct sData{public:  sData(int v){ data = new int; *data = v;}  sData& operator  + (sData d){     *data += *d.data;    return *this;  }public:  int * data;};  Note the sData& in your C++ code. That's a reference. A return value of a function (or operator) in Pascal is never a reference (ignoring some implementation details situations here which might have the appearance that it does, but from a language point of view it isn't). Quote from: Martin_fr on May 27, 2025, 09:42:15 amresult works like an "out param". It is not passed in. So the result in your operator is not initialized, it is a new uninitialised local variable. And only when your operator returns, then it is assigned to c (overwriting the content of c). This is even true for "operator :=". This will assign (e.g. cast, or in whatever way create the data) to a local "result". And that local result is then copied over to the variable in the caller. It's not quite right. Depending on the record it might indeed work like an out-parameter (or more precisely a var-parameter) and then the code would work; e.g. a record that contains a managed type will be passed like this, while others will use a copy. ---------------------------------------------------------------------- Martin_fr · 2025-05-27 20:28 Quote from: PascalDragon on May 27, 2025, 10:07:41 pmIt's not quite right. Depending on the record it might indeed work like an out- parameter (or more precisely a var-parameter) and then the code would work; e.g. a record that contains a managed type will be passed like this, while others will use a copy. Are you sure that isn't misleading too? Or I am overlooking something, and it might be considered a severe side effect.... Afaik, implementation detail, some result param are var param. But, they always get a temp var passed from the caller. So you don't have a reference to the callers var. Which from a users point of view makes it an out param. Otherwise, If I am assigning the returned result to a global var, that global vars content could change, before the result is returned. And that afaik it should not (actually must not?) At least below, the record is not behaving like a var param: 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 program Project1;{$Mode objfpc}type  TFoo = record s, s2,s3,s4,s5: ansistring; end;var   a: tfoo; function x: tfoo;begin  Result.s := StringOfChar('a', 10+Random(2));  writeln( a.s); // Does write bbbbend; begin  a.s := StringOfChar('b', 2+Random(2));  a := x; // a is not passed as var paramend.  Even though, implementation wise it is, but with a temp var. Of course there is a case were it can be observed. If peeking at uninitialized values. When calling "a:=x;" twice, the 2nd call will see the returned value of the 1st call. ---------------------------------------------------------------------- PascalDragon · 2025-05-27 20:37 Quote from: Martin_fr on May 27, 2025, 10:28:41 pmQuote from: PascalDragon on May 27, 2025, 10:07:41 pmIt's not quite right. Depending on the record it might indeed work like an out-parameter (or more precisely a var-parameter) and then the code would work; e.g. a record that contains a managed type will be passed like this, while others will use a copy. Are you sure that isn't misleading too? Or I am overlooking something, and it might be considered a severe side effect.... Afaik, implementation detail, some result param are var param. But, they always get a temp var passed from the caller. So you don't have a reference to the callers var. It's indeed very much an implementation detail, but it nevertheless does not change that the compiler does this. And even if the compiler currently does not optimize it to pass the destination variable directly it might or could do so in the future. Quote from: Martin_fr on May 27, 2025, 10:28:41 pmWhich from a users point of view makes it an out param. But only as long as they don't call the function multiple times in a row, because then the compiler will reuse the temporary variable...