Array or Ptr for UDT?

General FreeBASIC programming questions.
BasicScience
Posts: 475
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Array or Ptr for UDT?

Postby BasicScience » Jan 11, 2014 22:04

When using the data structure of a UDT for multiple copies, is there a reason to choose pointer arithmetic over an array allocation?

Since the UDT would typically be allocated as SHARED in a .BI file, the whole array would not need to be passed to s SUB or FUNCITON so it doesn't seem as though there are any advantages with the pointer method for which just the pointer would be passed.

Sample code snippet:

Code: Select all

const Max_Foo = 10

Type _Foo
    x  as single
    y  as single
END TYPE

Dim Shared MyFooPtr as _Foo Ptr
MyFooPtr = CALLocate (Max_Foo*len(_Foo))             

Dim Shared MyFooArray(Max_Foo) as _Foo

fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array or Ptr for UDT?

Postby fxm » Jan 11, 2014 22:43

For UDT, I prefer use New...Delete or New[]...Delete[] than Callocate()...Deallocate():

Code: Select all

const Max_Foo = 10

Type _Foo
    x  as single
    y  as single
END TYPE

Dim Shared MyFooPtr as _Foo Ptr
'MyFooPtr = CALLocate (Max_Foo*len(_Foo))
MyFooPtr = New _Foo[Max_Foo]

Dim Shared MyFooArray(Max_Foo-1) as _Foo
fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array or Ptr for UDT?

Postby fxm » Jan 11, 2014 23:10

For an array, it is only the descriptor which is passed by reference. So the passed data size in the stack is equivalent to passing a pointer.
BasicScience
Posts: 475
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Re: Array or Ptr for UDT?

Postby BasicScience » Jan 12, 2014 16:49

Got it, thanks.

Return to “General”

Who is online

Users browsing this forum: MSN [Bot] and 5 guests