Variable-length Array inside UDTs

New to FreeBASIC? Post your questions here.
Moot
Posts: 6
Joined: Sep 19, 2009 23:59
Location: Roanoke

Variable-length Array inside UDTs

Postby Moot » Sep 20, 2009 19:37

I understand you can't redim a regular array within a UDT, it has to be fixed length.

However is it possible to use a union of a fixed length (say for example 1024 bytes) inside of a UDT? So if I wanted an array 32 bytes long I would just uilize that much space in the union, and if I wanted an array of say, 1024 bytes long I would just use the entire union?
rdc
Posts: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Postby rdc » Sep 20, 2009 21:45

You wouldn't need a union for that. Just dim a large array and then use a count variable to indicate the number of elements.

A better way though is to use a pointer array. You need to have a data type pointer and counter:

Code: Select all

_listcount As Integer 'The number of items in list.
_list As ZString Ptr Ptr 'List of pointers to zstrings.


The just allocate the necessary elements as needed.

Code: Select all

'We'll need this for the zstring.
itemlen = Len(item)   
_listcount += 1
'Add a new item to the list.
_list = ReAllocate (_list, _listcount * SizeOf (ZString Ptr))
'Make sure we have a valid pointer.
If _list Then
  'Create a new zstring. Add a space for the null terminator.
   _list[_listcount - 1] = Allocate(itemlen + 1)
End If
Moot
Posts: 6
Joined: Sep 19, 2009 23:59
Location: Roanoke

Postby Moot » Sep 20, 2009 22:03

I'm building a search Tree for a chess algorithm but your solution works for my purpose with a little modification, thanks :)
Hard
Posts: 135
Joined: Aug 29, 2008 21:13

Postby Hard » Sep 20, 2009 23:07

have a look at linked lists. maybe they also perform better for you than a regular array.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests