BBC BASIC for Windows
Programming >> BBC BASIC language >> Double LOCAL
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1385106377

Double LOCAL
Post by Matt on Nov 22nd, 2013, 06:46am

Hi,

Did I read somewhere (I can't find any evidence of it) that you shouldn't, or wouldn't normally, use LOCAL for a variable in a proc and then LOCAL while DIMming it?

E.G. (from help)
[DEF PROC_TEST]
LOCAL A%
DIM A% LOCAL 67
...

The help indicates that you should, so I'm taking that as true, but I'm sure there was an example somewhere, similar to the above, that doesn't / shouldn't use the second one. Am I just imagining this?

Matt
Re: Double LOCAL
Post by admin on Nov 22nd, 2013, 08:36am

on Nov 22nd, 2013, 06:46am, Matt wrote:
The help indicates that you should, so I'm taking that as true, but I'm sure there was an example somewhere, similar to the above, that doesn't / shouldn't use the second one. Am I just imagining this?

I can't comment on the example without seeing it, but this is just one of those cases where a keyword - LOCAL - has multiple different meanings depending on the context.

In LOCAL A% the LOCAL means that the variable A% should be saved temporarily during the execution of the FN/PROC, and restored on exit, so that it does not affect the use of that same variable in the code from which the FN/PROC is called.

In DIM A% LOCAL 67 the LOCAL means that the memory is allocated from the stack rather than from the heap. Memory allocated from the stack is freed when the FN/PROC exits, but memory allocated from the heap isn't.

So the two uses of LOCAL are completely different and independent. The only thing they share is that in both cases they may only be used inside a FN/PROC, so for that reason you will commonly see them used together.

It is not uncommon in BBC BASIC for a keyword to be used in multiple different ways like this (for example RETURN has two quite different meanings). You might perhaps argue that using different keywords would be clearer, but given the limited number of keyword 'tokens' available you can understand the temptation to reuse them.

Richard.

Re: Double LOCAL
Post by Matt on Nov 23rd, 2013, 05:27am

on Nov 22nd, 2013, 08:36am, Richard Russell wrote:
In LOCAL A% the LOCAL means that the variable A% should be saved temporarily during the execution of the FN/PROC, and restored on exit, so that it does not affect the use of that same variable in the code from which the FN/PROC is called.

In DIM A% LOCAL 67 the LOCAL means that the memory is allocated from the stack rather than from the heap. Memory allocated from the stack is freed when the FN/PROC exits, but memory allocated from the heap isn't.


OK. That makes sense. But does that mean that DIM A% 67 allocates memory from the heap and therefore isn't freed on exit from a FN/PROC?

Matt
Re: Double LOCAL
Post by admin on Nov 23rd, 2013, 09:50am

on Nov 23rd, 2013, 05:27am, Matt wrote:
But does that mean that DIM A% 67 allocates memory from the heap and therefore isn't freed on exit from a FN/PROC?

Absolutely. That doesn't mean one would never use it in a FN/PROC however. For example in the linked list article a FN is used to create a new node:

Code:
      DEF FNnewnode(RETURN n{})
      LOCAL P%
      DIM P% DIM(n{})-1
      !(^n{}+4) = P%
      = P% 

Here it's obviously correct that the memory block is allocated from the heap and not freed on exit.

Richard.