View previous topic :: View next topic |
Author |
Message |
Per Svensson
Joined: 03 Oct 2004 Posts: 235 Location: Gothenburg, Sweden
|
Posted: Thu Dec 10, 2015 11:22 am Post subject: Local arrays - Allowed? |
|
|
Hi all
Although having used Bascom for almost twelve years there are still things to wonder around (L)
Yesterday I saw that I in an old routine had passed Array variables to a sub just because I needed it for temporary use there.
It seemed a bit "untidy" to mess up a call like that just because I needed at temporary Array so I started investigateing whether local arrays are allowed or not.
Config Submode = New 'No need to declare subs
Config Base = 0 'Array indexing begin at zero
Dim Result As Byte
Dim Arr1(3) As Integer
Function Test(a() As Integer ) As Byte
'Increase all elements of A and return these in B. Then do some test on B (then scrap it)
'Yeah - I know this is nonsense, but just an illustration of using local arrays
Local B(3) As Integer
Call Arrsum(a(0) , B(0))
Test = 0
If B(1) > 5 Then Test = 1 'After this we do not need B anymore
End Function
'-------------------------------------------------
Sub Arrsum(a() As Integer , B() As Integer )
Local I As Byte
For I = 0 To 2
B(i) = A(i) + 1
Next I
End Sub
'-------------------------------------------------
Result = Test(arr1(0))
Stop
As you can see, array B is only used inside function TEST so dim'ing it would be a waste of RAM.
Compiler will not complain about the "LOCAL B(3) as INTEGER"
but throws an error "not dimensioned" when we try to pass the B to the Arrsum routine.
Now the question is :
Are local arrays allowed?
If not - then why no error when declaring B as local?
I know that Mark knows the answer straight away, but I really can't bother him every time...
(I use a special version of 2.0.7.8 with support for local constants. There is of course a chanse that this has some impact on the above?)
/Per
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Dec 10, 2015 11:27 am Post subject: |
|
|
from the help : The AT , ERAM, SRAM, XRAM directives can not be used with a local DIM statement. Also local arrays are not possible. _________________ Mark |
|
Back to top |
|
|
Per Svensson
Joined: 03 Oct 2004 Posts: 235 Location: Gothenburg, Sweden
|
Posted: Thu Dec 10, 2015 11:47 am Post subject: |
|
|
Can't find that in the Help Mark.
Where does it state that arrays can not be local?
And most important. Why is that so?
Another long desired feature is the ability to let functions return entire arrays.
A() = Myfunction( ....)
/Per |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Dec 10, 2015 11:56 am Post subject: |
|
|
check the LOCAL topic in the help : set cursor on LOCAL and press F1 _________________ Mark |
|
Back to top |
|
|
Per Svensson
Joined: 03 Oct 2004 Posts: 235 Location: Gothenburg, Sweden
|
Posted: Thu Dec 10, 2015 12:48 pm Post subject: |
|
|
Yes... and not a Word about arrays. The text only mention what TYPES there can be for a Local.
"There can be only LOCAL variables of the type BYTE, INTEGER, WORD, DWORD, LONG, SINGLE, DOUBLE or STRING.
A LOCAL variable is a temporary variable that is stored on the frame.
When the SUB or FUNCTION is terminated, the memory will be released back to the system.
A Sub/Function is full reentrant which means that it can be called recursively. Because of this, local memory is dynamic and not static as global variables.
BIT variables are not possible because they are GLOBAL to the system..."
And as the compiler also accepts that I declare a local Array within a sub..Well, what shouldl I Think?
/P |
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
|
Back to top |
|
|
Per Svensson
Joined: 03 Oct 2004 Posts: 235 Location: Gothenburg, Sweden
|
Posted: Thu Dec 10, 2015 1:04 pm Post subject: |
|
|
Ooops! Ok!
Some coffe may be needed. Or new spectacles
But still. The compiler accepts the LOCAL declaration!
Why?
/P |
|
Back to top |
|
|
|