[SOLVED] Passed array Global or pointer ?

General FreeBASIC programming questions.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: [SOLVED] Passed array Global or pointer ?

Post by exagonx »

speedfixer wrote:Regarding previous code:

I tried to make it small. I just can't leave something incomplete. Leftover from my teaching days, I think.

The topic - passed array Global or pointer: first line of code is the key. It points to the array to be stored in keepstring().
All other code uses that pointer.
If any part is not clear or there is a question of why I did something the way I did: please ask.

david
Thank you, you were really nice.

I figured out how to use pointers with this example and it seems to be necessary to use a SHARED global pointer if you are using a lot of sub functions and classes.

At this point I am undecided when I create a function, which is the most correct way to opt for a ByRef or create a pointer, it gives me the impression of the difference between IF and CASE.

In any case, thanks again.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [SOLVED] Passed array Global or pointer ?

Post by fxm »

If you use a pointer to access data in an array, this pointer should only be updated each time the array is resized (by REDIM):
- in all other cases (read / write data), the data address of the array is not modified,
- only when resizing the array can the memory allocated for array data be reallocated.

Therefore, for a fix-len array, it is sufficient to initialize the pointer only once (before its first use to access data).

This is true for any type of array, even for a string array, because in this last case, the array data are first the string descriptors and not directly the character data.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: [SOLVED] Passed array Global or pointer ?

Post by exagonx »

fxm wrote:If you use a pointer to access data in an array, this pointer should only be updated each time the array is resized (by REDIM):
- in all other cases (read / write data), the data address of the array is not modified,
- only when resizing the array can the memory allocated for array data be reallocated.

Therefore, for a fix-len array, it is sufficient to initialize the pointer only once (before its first use to access data).
Thanks for the clarification, in the code below since it is used in a class, the Array in question is as if it were a global for the elements within the class so as long as everything is handled within the class the pointers I do not need or am I wrong ?

Code: Select all

type myclass
   redim as string listname(0 to 100) 
   dim as integer listname_index = 100
   declare sub addspace(byval myspace as integer)
   

end type
sub myclass.addspace(byval myspace as integer)
   dim TempArray(this.listname_index) as string
   dim Cpy as integer
   dim NewLen as integer = (this.listname_index + myspace)
   dim OldLen as integer = (this.listname_index)
   
   
   for Cpy = 0 to this.listname_index step 1
      TempArray(Cpy) = this.listname(Cpy)
   next Cpy
   redim this.listname ( NewLen ) as string
   for Cpy = 0 to this.listname_index step 1
      this.listname(Cpy) = TempArray(Cpy)
   next Cpy
   this.listname_index = this.listname_index + myspace
end sub

 dim nextrow as myclass


dim sto as integer
for sto = 0 to nextrow.listname_index step 1
   nextrow.listname(sto) = "casual=" & int(sto)
next sto
nextrow.addspace(1)
nextrow.listname(nextrow.listname_index) = "end record"
for sto = 0 to nextrow.listname_index step 1
print nextrow.listname(sto)
next sto
Last edited by exagonx on Oct 21, 2021 9:03, edited 1 time in total.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [SOLVED] Passed array Global or pointer ?

Post by fxm »

myclass.listname() is declared as a fix-len array.
Therefore, you cannot resize it.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: [SOLVED] Passed array Global or pointer ?

Post by exagonx »

fxm wrote:myclass.listname() is declared as a fix-len array.
Therefore, you cannot resize it.
Yes I wrote directly on the post and I published without checking the code, thanks I corrected the error
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [SOLVED] Passed array Global or pointer ?

Post by fxm »

You can simplify the code by using 'REDIM PRESERVE':

Code: Select all

sub myclass.addspace(byval myspace as integer)
   this.listname_index = this.listname_index + myspace
   redim preserve this.listname ( this.listname_index )
end sub
  • Note: when you resize an array, it is not necessary to re-declare its type ('as string').
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: [SOLVED] Passed array Global or pointer ?

Post by exagonx »

fxm wrote:You can simplify the code by using 'REDIM PRESERVE':

Code: Select all

sub myclass.addspace(byval myspace as integer)
   this.listname_index = this.listname_index + myspace
   redim preserve this.listname ( this.listname_index )
end sub
Thanks, you're right I didn't think about it.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: [SOLVED] Passed array Global or pointer ?

Post by speedfixer »

Thanks, fxm. Forgot that point. I rarely use fixed sized arrays, anymore.

If you want the variable sized array (start small then grow) - you need to test for a full array, then resize it.
A REDIM can take a lot of time, especially when it gets large. Always grow; never shrink. That just adds to the complexity.

With my previous example, I fail the assignment if the array were full. That is the point the array would be REDIM PRESERVE and the pointer reassigned. With a simple variable array, it shouldn't move. To be future-proof, I always reassign anyway.

Just declare the array with (any) instead of a dimension and have the code completely manage the array size. Storage is different in this case.

Next challenge: chasing a multi-dimension array with pointers.

david
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: [SOLVED] Passed array Global or pointer ?

Post by exagonx »

speedfixer wrote: Just declare the array with (any) instead of a dimension and have the code completely manage the array size. Storage is different in this case.

david
yes I have seen that the redim slows down the code, but as you well know the data always tends to increase, and currently I have two solutions to solve this hitch, the first is to create a very large array but if you get to the limit you are done, the second instead it is a direct random access to a file, the second case is not easy to manage but you have no space problems.
Post Reply