Hello !
I didn't have found a satisfying answer in the archives to the following problem:
In BP the constructor of TObject resets all datafields to 0 by assuming a simple type to the SELF-pointer with the length SIZEOF(SELF). This allowes all further derived objects to reset their datafields just by calling
INHERITED TOBJECT.INIT(...);
in their constructors. Creating Objects in Delphi resolves also zero-initialized datafields.
GPC does not so when creating a simple Object by
NEW (OBJECTPOINTER, INIT(...));
Is there a way in GPC to do something similar to the TObject-Constructor from BP within the constructor of a basic object or could it be implemented in GPC that creating a new Object with NEW will zero the memory allocated for the new object ?
Greetings, Thomas
On 1 Nov 2003 at 22:18, ThomasM wrote:
Hello !
I didn't have found a satisfying answer in the archives to the following problem:
In BP the constructor of TObject resets all datafields to 0 by assuming a simple type to the SELF-pointer with the length SIZEOF(SELF). This allowes all further derived objects to reset their datafields just by calling
INHERITED TOBJECT.INIT(...);
in their constructors. Creating Objects in Delphi resolves also zero-initialized datafields.
GPC does not so when creating a simple Object by
NEW (OBJECTPOINTER, INIT(...));
Neither should it. In BP and Delphi, you are referring to base objects in a bundled class library. There is no such bundled class library with GPC.
Is there a way in GPC to do something similar to the TObject-Constructor from BP within the constructor of a basic object or could it be implemented in GPC that creating a new Object with NEW will zero the memory allocated for the new object ?
By callng FillChar (Self, Sizeof (Self), 0) of something to that effect (for an example, see the initialisation of TObject in the FreeVision objects unit - for a GPC compilable version, see: http://www.gnu-pascal.de/contrib/chief/objects/objects-20031019.zip)
With GPC, doing this may well trash the VMT (Frank can tell you whether that is so or not). It will most certainly trash all fields that are strings. Therefore, you are still left with initialising everything manually.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.bigfoot.com/~african_chief/
Prof A Olowofoyeku (The African Chief) wrote:
By callng FillChar (Self, Sizeof (Self), 0) of something to that effect (for an example, see the initialisation of TObject in the FreeVision objects unit - for a GPC compilable version, see: http://www.gnu-pascal.de/contrib/chief/objects/objects-20031019.zip)
With GPC, doing this may well trash the VMT (Frank can tell you whether that is so or not).
Yes, it is:
program Foo;
type Foo = object constructor Init; end;
constructor Foo.Init; begin WriteLn (PtrInt (TypeOf (Self))); FillChar (Self, SizeOf (Self), 0); WriteLn (PtrInt (TypeOf (Self))) end;
var a: Foo;
begin a.Init end.
gives:
134775684 { or any other address } 0
Of course, you could save the VMT (SizeOf) and restore it afterwards (SetType), but ...
It will most certainly trash all fields that are strings.
And other schemata, and files, and objects (though objects as fields of another object may not be so common), and perhaps more in the future.
For non-object types you could use `Initialize' to re-init them, but in an object base type it only knows about the fields of the base type, so it won't help much.
Therefore, you are still left with initialising everything manually.
And doing this in each derived type as well.
It's generally much easier (and better style) to zero out those fields you really need to (which are usually not all fields).
Frank