BBC BASIC for Windows
« Double LOCAL »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 11:46pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Double LOCAL  (Read 700 times)
Matt
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Double LOCAL
« Thread started 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
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Double LOCAL
« Reply #1 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.
User IP Logged

Matt
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: Double LOCAL
« Reply #2 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
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Double LOCAL
« Reply #3 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.
« Last Edit: Nov 23rd, 2013, 09:50am by admin » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls