Forum > General

the address of Self in constructor?

(1/1)

marunguy:
Below is the output of test code.
Why are the addresss of instance(@a) and the address of Self(@Self) in constructor different?
I think both should be the same.
Is it possible to get the address equal to the addresss of instance(@a) in constructor?

Lazarus 2.2.0 64bit for windows.


--- Quote ---@Self=00000000013FFDC8
@a=00000000013FFE38
a.i=3
a_ptr^.i=3
ga_ptr^.i=5872  << invalid value
--- End quote ---


--- 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";}};} ---unit unitmain; {$mode ObjFPC}{$H+} interface uses    Classes, SysUtils; type    ClassA = class    public        constructor Create(i : Integer);        var            i : Integer;    end;    PClassA = ^ClassA; var    ga_ptr : PClassA; procedure TestSelfAddr(); implementation constructor ClassA.Create(i : Integer);begin    Self.i := i;    ga_ptr := @Self;    Writeln(Format('@Self=%P', [@Self]));end; procedure TestSelfAddr();var    a : ClassA = nil;    a_ptr : PClassA = nil;begin    a := ClassA.Create(3);    a_ptr := @a;     Writeln(Format('@a=%P', [@a]));    Writeln(Format('a.i=%d', [a.i]));    Writeln(Format('a_ptr^.i=%d', [a_ptr^.i]));    Writeln(Format('ga_ptr^.i=%d', [ga_ptr^.i]));   // << invalid valueend; end. 

bytebites:

--- 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";}};} ---unit unitmain; {$mode ObjFPC}{$H+} interface uses    Classes, SysUtils; type    ClassA = class    public        constructor Create(i : Integer);        var            i : Integer;    end;    PClassA = ^ClassA; var    ga_ptr : PClassA; //procedure TestSelfAddr(); implementation constructor ClassA.Create(i : Integer);begin    Self.i := i;    ga_ptr := pointer(Self);    Writeln(Format('Self=%P', [pointer(Self)]));end; //procedure TestSelfAddr();var    a : ClassA = nil;    a_ptr : PClassA = nil;begin    a := ClassA.Create(3);    a_ptr := @a;     Writeln(Format('a=%P', [pointer(a)]));    Writeln(Format('a.i=%d', [a.i]));    Writeln(Format('a_ptr^.i=%d', [a_ptr^.i]));    Writeln(Format('classa(ga_ptr).i=%d', [classa(ga_ptr).i]));//end; end. 

Zvoni:
"Self" itself is already a pointer.
Your .... [@Self] is a Pointer to a pointer

PascalDragon:

--- Quote from: marunguy on May 04, 2022, 07:09:32 am ---Why are the addresss of instance(@a) and the address of Self(@Self) in constructor different?
I think both should be the same.

--- End quote ---

Your assumptions are wrong as the others already hinted at. The address of Self is irrelevant, cause in case of your a it's the address of the variable on the stack and in case of @Self the compiler moves the value of Self which was likely passed in a register to the stack so that it can give you the address. Thus the address is different. The value however is the same in both cases and that is what you want; bytebites already showed how, though your ga_ptr should be declared as ClassA instead of PClassA (class variables already are pointers you don't need to use a pointer yourself and the cases where you really need a pointer to a class are very, very narrow).

Navigation

[0] Message Index

Go to full version