ReDimming a Shared array

General FreeBASIC programming questions.
deltarho[1859]
Posts: 2003
Joined: Jan 02, 2017 0:34
Location: UK

ReDimming a Shared array

Postby deltarho[1859] » Sep 18, 2019 12:47

I have a problem when trying to ReDim a Shared array.

I have, for example
Dim Shared array(1 to 32) As Wstring * 256 ...... [1]

At some point, in a function, I want to ReDim(1 To NumOfPaths)

If I use ReDim Preserve array(1 To NumOfPaths) As Wstring * 256 then nothing is Preserved.

If I use either
ReDim Shared Preserve array(1 To NumOfPaths) As Wstring * 256
or
ReDim Preserve Shared array(1 To NumOfPaths) As Wstring * 256

I get told 'Illegal inside functions, found 'shared' ...' .

OK, that makes sense so how do I Preserve the contents when using [1]?
fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ReDimming a Shared array

Postby fxm » Sep 18, 2019 12:58

deltarho[1859] wrote:I have, for example
Dim Shared array(1 to 32) As Wstring * 256 ...... [1]

As above declared, your array is a fix-len array (because declared "Dim" with integer literals as bounds).

To declare a var-len array, but sizing it at same time (with integer literals as bounds), the syntax is:
ReDim Shared array(1 to 32) As Wstring * 256

To resize it:
ReDim Preserve array(1 To NumOfPaths)
is sufficient.
Even to resize a var-len array declared "Shared", the "Shared" specifier is always forbidden of use when resizing the array (the only authorized specifier is "Preserve").
Last edited by fxm on Sep 18, 2019 14:12, edited 1 time in total.
deltarho[1859]
Posts: 2003
Joined: Jan 02, 2017 0:34
Location: UK

Re: ReDimming a Shared array

Postby deltarho[1859] » Sep 18, 2019 13:14

I am with it - the initial declaration should have been ReDim and not Dim.

All is well now.

Your response time always astounds me!

Thanks fxm.
speedfixer
Posts: 383
Joined: Nov 28, 2012 1:27
Location: California

Re: ReDimming a Shared array

Postby speedfixer » Sep 18, 2019 16:43

Another possibility:

dim shared as type name(any)

then change it later.
I use this more often.


Better:

dim as type name (any)
dim shared as type ptr pname

< more code >


redim name(0 to value)
pname = @name(0)

You must reassign pname on every redim - location of array may change.

Your pointer can be more easily 'common shared' to assist with working with multiple modules.

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

Re: ReDimming a Shared array

Postby fxm » Sep 18, 2019 17:54

I prefer to use:
Common Shared As datatype Array(Any)

Instead of sharing the address of the first element, it is better to share the address of the array descriptor (constant address), which makes it possible to resize the array from any module.
speedfixer
Posts: 383
Joined: Nov 28, 2012 1:27
Location: California

Re: ReDimming a Shared array

Postby speedfixer » Sep 18, 2019 20:15

True. Have used that method.

As some of my programs get larger and more complex, I share the pointer in global space and keep the data in a single function as static.

That function can deliver one member (typically a UDT) as the output of the function. I reserve the 0 index member as a keeper of info about the array usage.
I can direct the function to resize the array as I want, with validity tests and more built in.

Only one doorway for direct access. Much easier to track and no one can poke directly at the data structure. If the UDT is complex, helper functions manage access. Why push a whole descriptor around when one pointer will do?
So simple to actually put this all into a single 'class' object, if desired. Personally, I don't want the RTTI overhead, though there are times when that one class can ease the burden. My choice.

This is my way, always the same, so **I** always know what is going on. Convenient for me.

There are many ways to do this, though, and no one way is best for all.

david

Return to “General”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 19 guests