Implicit vs Explicit Reference/Dereference

General FreeBASIC programming questions.
Post Reply
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Implicit vs Explicit Reference/Dereference

Post by Munair »

From the manual and code examples I understand that objects are instantiated implicitly and destroyed automatically when they loose scope. But there is also the possibility to instantiate them explicitly with the keyword NEW, in which case they have to be destroyed explicitly using the keyword DELETE. OK, I understand that.

However, what is the real difference between the two? For instance, Is there a difference in member initialization? Child objects and other members are always instantiated automatically; the keyword NEW doesn't work on them, as far as I can tell.

I would like to be consistent in my approach, so if child objects cannot be instantiated explicitly, I would like to do the same for parent objects:

Code: Select all

dim Parent as TParent()
instead of

Code: Select all

dim Parent as TParent ptr = new TParent()
...
delete Parent
the latter of which would result in a mix of member symbol usage:

Code: Select all

Parent->Child.myname
Any insights would be greatly appreciated.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Implicit vs Explicit Reference/Dereference

Post by fxm »

Can you provide a code example of a structure with your parent and child to better understand your remarks and questions?
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Implicit vs Explicit Reference/Dereference

Post by Munair »

I stand corrected. explicit instantiation is allowed for child objects. Here is a working example. Is there any advantage with this approach over implicit instantiation?

Code: Select all

/'EXPLICIT INSTANTIATION'/
type TChild
  myname as string
end type

type TParent extends object
  Child as TChild ptr
  declare constructor()
  declare destructor()
end type

constructor TParent()
  Child = new TChild()
  Child->myname = "child"
end constructor

destructor TParent()
  delete Child
end destructor

dim Parent as TParent ptr  = new TParent()

print Parent->Child->myname
delete Parent
VERSUS

Code: Select all

/'IMPLICIT INSTANTIATION'/
type TChild
  myname as string
end type

type TParent extends object
  Child as TChild
end type

dim Parent as TParent

Parent.Child.myname = "child"
print Parent.Child.myname
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Implicit vs Explicit Reference/Dereference

Post by fxm »

When using dynamic instantiation, each "New" must be ended by a "Delete":

Code: Select all

/'DYNAMIC INSTANTIATION'/
type TChild
  myname as string
end type

type TParent extends object
  Child as TChild ptr
  declare constructor()
  declare destructor()
end type

constructor TParent()
  Child = new TChild()
  Child->myname = "child"
end constructor

destructor TParent()
  delete Child
end destructor

dim Parent as TParent ptr  = new TParent()

print Parent->Child->myname
delete Parent
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Implicit vs Explicit Reference/Dereference

Post by fxm »

When using static allocation ("Dim"), the used memory is reserved in one go before the beginning of the program, and freed in one go at the end of the program. Each instance is constructed/destructed at the input/output of its scope (without freeing the corresponding allocated memory).

When using dynamic allocation ("New"), the necessary memory is allocated just before the instance construction (triggered by the "New" keyword), and this memory is freed just after the instance destruction (triggered by the "Delete" keyword).
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Implicit vs Explicit Reference/Dereference

Post by Munair »

fxm wrote:When using static allocation ("Dim"), the used memory is reserved in one go before the beginning of the program, and freed at the end of the program. Each instance is constructed/destructed at the input/output of its scope (without freeing the allocated memory).

When using dynamic allocation ("New"), the memory is allocated just before the instance construction (triggered by the "New" keyword), and the memory is freed just after the instance destruction (triggered by the "Delete" keyword).
Interesting and good to know. Thanks fxm.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Implicit vs Explicit Reference/Dereference

Post by jj2007 »

Interesting... even a local string triggers a malloc/mfree sequence:

Code: Select all

Function hello3 (ct as integer) as Boolean
  Dim as string MyString3="Hello World "+str(ct)
  print MyString3
  Return 0
End Function
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Implicit vs Explicit Reference/Dereference

Post by fxm »

Yes, because a string is a predefined dynamic object (in terms of dynamic memory allocation for the string data characters).
Post Reply