Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 19:21 17 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Problem with Redimensioning a passed-in array

Author Message
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 405
Posted: 10:34pm 22 Feb 2021
Copy link to clipboard 
Print this post

Here is a little test program that shows the problem.

You can re-dimension an array easily by using ERASE and then DIM again. However, if you pass in an array to a subroutine and try to re-dimension the passed-in array inside the subroutine, things go bad.

Peter, any comments? Thanks!
-Bill


' Test for redimensioning an array that is passed
' into a subroutine, as opposed to in the main program.
' It appears that something goes wrong if you try to
' redimension a passed-in array inside the subroutine.
' William M Leue 2/22/2021

option default integer
option base 1

' start with a dummy size
dim array(2, 2, 2)
print "dimension 1: " + str$(bound(array(), 1))
print "dimension 2: " + str$(bound(array(), 2))
print "dimension 3: " + str$(bound(array(), 3))

' Here we redimension the array in the main program.
' This works fine.
erase array
dim array(10, 10, 10)

print "After redimensioning in main program"
print "dimension 1: " + str$(bound(array(), 1))
print "dimension 2: " + str$(bound(array(), 2))
print "dimension 3: " + str$(bound(array(), 3))

' Pass the array into the subroutine 'ReAllocate'
ReAllocate array()
end

' Here is the subroutine.
sub ReAllocate array()

 ' Verifying the dimensions of the passed-in array
 print "Looking at array inside the sub ReAllocate"
 print "dimension 1: " + str$(bound(array(), 1))
 print "dimension 2: " + str$(bound(array(), 2))
 print "dimension 3: " + str$(bound(array(), 3))

 ' Here is where things go wrong!
 ' The erase works but redimensioning produces the
 ' error: "Error in Line 43: array(5, 5, 5) already declared."
 erase array
 dim array(5, 5, 5)   '<=== This fails

 print "Looking at array inside the sub ReAllocate after redimensioning"
 print "dimension 1: " + str$(bound(array(), 1))
 print "dimension 2: " + str$(bound(array(), 2))
 print "dimension 3: " + str$(bound(array(), 3))

end sub

 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10591
Posted: 10:41pm 22 Feb 2021
Copy link to clipboard 
Print this post

  Quote  Peter, any comments? Thanks!


Don't do it  
 
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 405
Posted: 12:07am 23 Feb 2021
Copy link to clipboard 
Print this post

Haha! Well, if necessary, I can code around the problem. (bug?)

But it's too bad, I have a good use case for wanting to do this.

-Bill
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025