FreeBASIC syntax challenge games

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

Really helpful stuff fxm.

I'll let my crude method be my answer to the challenge.

Maybe other methods will unfold in due course.
Here I test the crude method under looping.
Any overwriting of true or false will show with draw string.
Tested gas, gcc, 32 and 64 bits optimised -O3 and un optimized.

Code: Select all


#macro TypeHasDtor(TypeSymbol, TypeHasDtorResult)
  randomize
    type udt##typesymbol
        as typesymbol  x
    end type
    
    dim as integer Num##typesymbol=2000+int(rnd*10000) 'arbitrary size
    dim  as integer ptr p0##typesymbol=new integer[1]                               'address 0
    dim  as udt##typesymbol ptr p1##typesymbol=new udt##typesymbol[Num##typesymbol] 'address 1
    dim as integer x##typesymbol= (cint(p1##typesymbol)-cint(p0##typesymbol))/sizeof(integer)
    'print x##typesymbol
    TypeHasDtorResult=iif(x##typesymbol and 1,true,false) 'if difference is odd then true
    delete[] p1##typesymbol
    delete[] p0##typesymbol
#endmacro



'looper
#macro TypeHasDtorrun(TypeSymbol, TypeHasDtorResult)
     Num##typesymbol=10+rnd*50 'an arbitrary number of instances
     p0##typesymbol=new integer[1]
     p1##typesymbol=new udt##typesymbol[Num##typesymbol]
     x##typesymbol= (cint(p1##typesymbol)-cint(p0##typesymbol))/sizeof(integer)
     TypeHasDtorResult=iif(x##typesymbol and 1,true,false) 'if difference is odd then true
    delete[] p1##typesymbol
    delete[] p0##typesymbol
    
#endmacro


Dim As boolean hasDtorResult

TypeHasDtor(Integer, hasDtorResult)
Print "The 'Integer' Type has a destructor (implicit or explicit): ", hasDtorResult

TypeHasDtor(String, hasDtorResult)
Print "The 'String' Type has a destructor (implicit or explicit): ", hasDtorResult

Type string10Type As String * 10  '' define an alias to get a symbol name
TypeHasDtor(string10Type, hasDtorResult)
Print "The 'String * 10' Type has a destructor (implicit or explicit): ", hasDtorResult

Type stringptrType As String Ptr  '' define an alias to get a symbol name
TypeHasDtor(stringptrType, hasDtorResult)
Print "The 'String Ptr' Type has a destructor (implicit or explicit): ", hasDtorResult

TypeHasDtor(Object, hasDtorResult)
Print "The 'Object' Type has a destructor (implicit or explicit): ", hasDtorResult

Type UDT1 Extends Object
End Type
TypeHasDtor(UDT1, hasDtorResult)
Print "The 'UDT1' User-Type has destructor (implicit or explicit): ", hasDtorResult

Type UDT2 Extends Object
    Declare Destructor ()
End Type
Destructor UDT2 ()
End Destructor
TypeHasDtor(UDT2, hasDtorResult)
Print "The 'UDT2' User-Type has destructor (implicit or explicit): ", hasDtorResult
'====================================
screen 19

dim as long counter
do
    counter+=1

TypeHasDtorRun(Integer, hasDtorResult)
draw string(0,30), "The 'Integer' Type has a destructor (implicit or explicit):      " & hasDtorResult

TypeHasDtorRun(String, hasDtorResult)
draw string(0,50), "The 'String' Type has a destructor (implicit or explicit):       "& hasDtorResult


TypeHasDtorRun(string10Type, hasDtorResult)
draw string(0,70), "The 'String * 10' Type has a destructor (implicit or explicit):  "& hasDtorResult


TypeHasDtorRun(stringptrType, hasDtorResult)
draw string(0,90), "The 'String Ptr' Type has a destructor (implicit or explicit):   "& hasDtorResult

TypeHasDtorRun(Object, hasDtorResult)
draw string(0,110),"The 'Object' Type has a destructor (implicit or explicit):       "& hasDtorResult

TypeHasDtorRun(UDT1, hasDtorResult)
draw string(0,130), "The 'UDT1' User-Type has destructor (implicit or explicit):      "& hasDtorResult


TypeHasDtorRun(UDT2, hasDtorResult)
draw string(0,150), "The 'UDT2' User-Type has destructor (implicit or explicit):      "& hasDtorResult
locate 12
print "Runs ",counter
loop

sleep
 
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

Variant on the same idea. Maybe what I was thinking of in my first tries I guess (and still may well be very wrong! when will he learn ahah!):

Code: Select all

destructor DESTR_UDT()
   this.dii = -111
end destructor

type TESTOBJ as string

type BOTTLE
   as TESTOBJ v
   as integer u = 999998
end type

dim as BOTTLE ptr bb = new BOTTLE[1]

? cast(integer ptr, @bb->u)[0 - sizeOf(TESTOBJ)/sizeOf(integer) - 1]

delete[] bb

? cast(integer ptr, @bb->u)[0 - sizeOf(TESTOBJ)/sizeOf(integer) - 1]
1
10824432
Appuyez sur une touche pour continuer...
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Tourist Trap wrote: But about the last challenge, following the insight from dodicat, what if we try a placement new[] pointing in the middle of an array? I tried that, when I delete[] at one index, things are messed up in all the array
See at Operator Placement New documentation page.

Important extract of this documentation page:
Memory is not allocated when using the Placement New operator. Instead, the memory at the specified address is used (the provided memory size must be large enough to contain all the placement).
It is incorrect to call Delete Statement on the address. The proper way is to only call the destructor if one exists (implicitly or explicitly), with syntax as for a member method by using member access operator.
See examples below for proper Placement New operator usage.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

fxm wrote: Important extract of this documentation page:
Memory is not allocated when using the Placement New operator. Instead, the memory at the specified address is used (the provided memory size must be large enough to contain all the placement).
It is incorrect to call Delete Statement on the address. The proper way is to only call the destructor if one exists (implicitly or explicitly), with syntax as for a member method by using member access operator.
See examples below for proper Placement New operator usage.
Ok, that makes sense. However, why can't I see the extra destructor flag around (ahead) - I would be expecting it somewhere at the extra index of the array. My understanding was that it spawns with NEW[] (placement or not).
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Placement New [] does not write the number of elements at the beginning of the memory block, because Delete [] does not need to be and must not be called.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

fxm wrote:Placement New [] does not write the number of elements at the beginning of the memory block, because Delete [] does not have to be and must not be called.
Fxm, I was reading the page on New/Delete of the wiki. You write:
Instance array created with 'Operator New[] Overload/Expression' must be freed with 'Operator Delete[] Overload/Statement', the array-version of 'Operator Delete Overload/Statement'.
And:
User can not mix and match the different versions of the operators.
I understand that you mean: don't mix NEW[] and DELETE, or NEW and DELETE[], but you don't mention the case we face here with "placement new". It's not a problem because there is no allocation, but the mechanism in the case of PLACEMENT NEW[] and NEW[] differs, and here the term 'must be freed' looks contradictory at first sight. If I understand well now, here you must not use DELETE[] at all.

However in the destructor page (a quite useful page!), you eventually mention all this:
Destructor can be also called directly from the typename instance like the other member methods (Sub) and with the same syntax, i.e. using a member access operator, e.g. obj.Destructor(). The object, and all its members, are assumed to be constructed and in a valid state, otherwise its effects are undefined and may cause crashes. This syntax is useful in cases where obj has been constructed manually, e.g. with obj.constructor() or Placement New.
So I guess it's ok overall :)

ps: I don't want to develop here too far requests concerning the documentation, just I observe that there is no mention made of the possibility to call explicitely the destructor as any usual subroutine. Again, a detail, even if I can imagine that it may be misleading sometimes. But ok for now from my side. sorry misread! everything there just me need bigger eyes ;)
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Tourist Trap wrote: Fxm, I was reading the page on New/Delete of the wiki. You write:
Instance array created with 'Operator New[] Overload/Expression' must be freed with 'Operator Delete[] Overload/Statement', the array-version of 'Operator Delete Overload/Statement'.
And:
User can not mix and match the different versions of the operators.
I understand that you mean: don't mix NEW[] and DELETE, or NEW and DELETE[], but you don't mention the case we face here with "placement new". It's not a problem because there is no allocation, but the mechanism in the case of PLACEMENT NEW[] and NEW[] differs, and here the term 'must be freed' looks contradictory at first sight. If I understand well now, here you must not use DELETE[] at all.
These sentences do not apply to the operator placement New (the last in the list at page top).
I will add a clarification in this Programmer' Guide page.

[edit]
Done:
ProPgNewDelete → fxm [added clarification on 'Operator Placement New/New[]']
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

fxm wrote: [edit]
Done:
ProPgNewDelete → fxm [added clarification on 'Operator Placement New/New[]']
For me it's clearer. Thanks.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

What was your own solution to this latest challenge fxm?
sacho3 was pretty good at solving your challenges, but it has been nearly a year since he last posted, I hope he hasn't joined the growing ranks of the gone for good.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

I have given my solution here.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

Thanks fxm, I missed it.
Post Reply