Just starting out? Need help? Post your questions and find answers here.
helpy
Enthusiast
Posts: 552 Joined: Sat Jun 28, 2003 12:01 am
Post
by helpy » Wed Aug 16, 2017 10:20 pm
Example:
Code: Select all
EnableExplicit
Structure tItem
type.i
id.s
name.s
EndStructure
Define NewList ListOfItems.tItem()
Define NewList *ListOfItemPointers.tItem()
Procedure FillListOfItems(List Items.tItem(), SizeOfList=10)
Protected i
For i = 1 To SizeOfList
AddElement(Items())
With Items()
\id = RSet(Str(i),8,"0")
\name = "ListOfItems::0x" + RSet(Hex(i),8,"0")
\type = Random($7FFFFFFF)
EndWith
Next
EndProcedure
Procedure FillListOfItemPointers(List *Items.tItem(), SizeOfList=10)
Protected i, *item.tItem
For i = 1 To SizeOfList
*item = AllocateStructure(tItem)
If *item
With *item
\id = RSet(Str(i),8,"0")
\name = "ListOfItemPointers::0x" + RSet(Hex(i),8,"0")
\type = Random($7FFFFFFF)
EndWith
AddElement(*Items())
*Items() = *item
EndIf
Next
EndProcedure
Procedure DebugListOfItems(List Items.tItem())
Debug ">>>>>>>>>> Debug ListOfItems >>>>>>>>>>"
ForEach Items()
Debug Items()\name
Next
EndProcedure
Procedure DebugListOfItemPointers(List *Items.tItem())
Debug ">>>>>>>>>> Debug ListOfItemPointers >>>>>>>>>>"
ForEach *Items()
Debug *Items()\name
Next
EndProcedure
FillListOfItems(ListOfItems())
FillListOfItemPointers(*ListOfItemPointers())
DebugListOfItems(ListOfItems())
DebugListOfItemPointers(*ListOfItemPointers())
Debug ""
Debug ""
Debug "Wrong use of the procedures:"
Debug ""
DebugListOfItems(*ListOfItemPointers())
DebugListOfItemPointers(ListOfItems())
Shouldn't the compiler report, that a wrong list is passed to a procedure.
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
mk-soft
Always Here
Posts: 5409 Joined: Fri May 12, 2006 6:51 pm
Location: Germany
Post
by mk-soft » Wed Aug 16, 2017 10:37 pm
You can not switch the list
Code: Select all
;DebugListOfItems(*ListOfItemPointers())
;DebugListOfItemPointers(ListOfItems())
DebugListOfItems(ListOfItems())
DebugListOfItemPointers(*ListOfItemPointers())
Akzept only the same list type
helpy
Enthusiast
Posts: 552 Joined: Sat Jun 28, 2003 12:01 am
Post
by helpy » Wed Aug 16, 2017 10:47 pm
mk-soft wrote: You can not switch the list
I know!
My question is:
Shouldn't the compiler warn the user?
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
mk-soft
Always Here
Posts: 5409 Joined: Fri May 12, 2006 6:51 pm
Location: Germany
Post
by mk-soft » Wed Aug 16, 2017 11:08 pm
I believe in this case that it is too similar.
It is the same structure.
Compiler warning bug?
normeus
Enthusiast
Posts: 415 Joined: Fri Apr 20, 2012 8:09 pm
Contact:
Post
by normeus » Thu Aug 17, 2017 12:00 am
Smaller example showing that a list as a parameter in a procedure is just a pointer to a list.
look at my "Declare"
Code: Select all
EnableExplicit
NewList goodpeople()
NewList *good()
Declare procgoodpeople(List goodpeople())
Procedure procgoodpeople( List *good())
AddElement(*good())
*good()=1+*good()
EndProcedure
AddElement(*good())
procgoodpeople(goodpeople())
Debug goodpeople()
Debug ListSize(goodpeople())
Debug ListSize(*good())
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
helpy
Enthusiast
Posts: 552 Joined: Sat Jun 28, 2003 12:01 am
Post
by helpy » Thu Aug 17, 2017 2:15 pm
But the following lists are stored completley differently in memory:
Code: Select all
Define NewList ListOfItems.tItem()
Define NewList *ListOfItemPointers.tItem()
AddElement to ListOfItems will automatically allocate memory with SizeOf(tItem) for each element.
AddElement to *ListOfItemPointers will only allocate an integer for each new element.
The example of normeus is not the same!
Code: Select all
[code]NewList goodpeople()
NewList *good()
[/code]Both lists goodpeople() and *good() work the same because element size has the size of an integer for both lists!
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
normeus
Enthusiast
Posts: 415 Joined: Fri Apr 20, 2012 8:09 pm
Contact:
Post
by normeus » Thu Aug 17, 2017 7:25 pm
Sorry to make it confusing.
I was trying to point out that when you define a parameter as a list of pointers then you can send any list to the procedure.
I used this "Feature" before. You can see that my declare does not match my procedure at all. Just be aware of this.
Code: Select all
EnableExplicit
Structure earth
northAmerica.i
SouthAmerica.i
europe.i
africa.i
asia.i
EndStructure
Structure moon
moonpeople.s
EndStructure
NewList goodpeople()
NewList *good.earth()
Declare procgoodpeople(List goodpeople.s())
Procedure procgoodpeople( List *good())
AddElement(*good())
*good()=1+*good()
EndProcedure
AddElement(*good())
procgoodpeople(goodpeople())
Debug goodpeople()
Debug ListSize(goodpeople())
Debug ListSize(*good())
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari