List of structure items vs. Pointer list of structure items

Just starting out? Need help? Post your questions and find answers here.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

List of structure items vs. Pointer list of structure items

Post by helpy »

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
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: List of structure items vs. Pointer list of structure it

Post by mk-soft »

You can not switch the list

Code: Select all

;DebugListOfItems(*ListOfItemPointers())
;DebugListOfItemPointers(ListOfItems())
DebugListOfItems(ListOfItems())
DebugListOfItemPointers(*ListOfItemPointers())
Akzept only the same list type
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: List of structure items vs. Pointer list of structure it

Post by helpy »

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
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: List of structure items vs. Pointer list of structure it

Post by mk-soft »

I believe in this case that it is too similar.
It is the same structure.

Compiler warning bug?
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
normeus
Enthusiast
Enthusiast
Posts: 415
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: List of structure items vs. Pointer list of structure it

Post by normeus »

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
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: List of structure items vs. Pointer list of structure it

Post by helpy »

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
Enthusiast
Posts: 415
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: List of structure items vs. Pointer list of structure it

Post by normeus »

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
Post Reply