Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.sys.acorn.programmer > #5509
| Date | 2018-09-05 08:03 +0100 |
|---|---|
| From | Matthew Phillips <spam2011m@yahoo.co.uk> |
| Newsgroups | comp.sys.acorn.programmer |
| Subject | Re: how to use a sysvar in Basic |
| Message-ID | <2d4ea73257.Matthew@sinenomine.freeserve.co.uk> (permalink) |
| References | <d1fd503257.jim@6.abbeypress.net> <57325ba594UCEbin@tiscali.co.uk> <ab78703257.Alan.Adams@ArmX6.adamshome.org.uk> |
In message <ab78703257.Alan.Adams@ArmX6.adamshome.org.uk> on 4 Sep 2018 Alan Adams wrote: > In message <57325ba594UCEbin@tiscali.co.uk> > "John Williams (News)" <UCEbin@tiscali.co.uk> wrote: > > > In article <d1fd503257.jim@6.abbeypress.net>, > > Jim Nagel <jimnewsm13c@abbeypress.co.uk> wrote: > > >> Leads me to think there's something fundamental I need to understand > >> about the use of sysvars within Basic. > > > I have saved this useful snippet for further use: > > > <quote> > > On Tue, 06 Oct 2009 17:17:52 +0100, Alan Adams <alan@adamshome.org.uk> > > wrote: > > >>[...] > >> > >>DEF PROCsetsysvar(varname$,varval$) > >>LOCAL vallen% > >>LOCAL name% , val% > >>DIM name% LEN(varname$)+1, val% LEN(varname$)+1 > >>$name%=varname$+CHR$(0) > >>$val%=varval$+CHR$(0) > >>vallen%=LEN(varval$) > >>SYS "OS_SetVarVal",name%,val%,vallen%,0,0 > >>ENDPROC > > > There's no need whatsoever for the explicit DIMming and > > 0-termination. On SYS calls, BASIC already takes care > > of that for you (if there's a string parameter, its > > 0-terminated value is copied to the BASIC stack, and > > discarded after the SYS call). > > I wrote it that way because from the point of view of the BASIC manual > that behaviour is undefined. Strange. In the BBC BASIC Guide published in 1988 for the Archimedes 400 series under the description of the SYS command it says "if the expression evaluates to a string, the string is placed on BASIC's stack, beginning at a word boundary and terminated with a null character. A pointer to it is put in the register." There were other problems with the routine which John Kortink did not point out. The most serious was that val% is being dimensioned to the length of varname$ not varval$. Another is that, because you are adding a null character after the string, you actually need LEN(varval$)+2 because placing varval$+CHR$(0) in the buffer will actually add a terminating return character after the CHR$(0). Finally, every time the routine is used it will leak memory, because the memory allocated by DIM is not freed by BASIC afterwards. It is only the references to it in name% and val% that are discarded. This is not easily fixed across all versions of BASIC except by using a global buffer, but newer versions of BASIC have DIM LOCAL: DIM name% LOCAL LEN(varname$)+2 I'm not sure which branches of RISC OS have this and when it was introduced. Sorry to go over old code -- we've all moved on since then and learned more -- but I thought I'd better mention it in case you still have this routine in use on the "if it ain't broke don't fix it" principle. -- Matthew Phillips Durham
Back to comp.sys.acorn.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to use a sysvar in Basic Jim Nagel <jimnewsm13c@abbeypress.co.uk> - 2018-09-04 16:20 +0100
Re: how to use a sysvar in Basic Steve Drain <steve@kappa.me.uk> - 2018-09-04 18:00 +0100
Re: how to use a sysvar in Basic Jim Nagel <jimnewsm13c@abbeypress.co.uk> - 2018-09-04 18:54 +0100
Re: how to use a sysvar in Basic Julian Fry <julianfry@orpheusmail.co.uk> - 2018-09-08 21:03 +0100
Re: how to use a sysvar in Basic Jim Nagel <jimnewsm13c@abbeypress.co.uk> - 2018-09-05 17:13 +0100
Re: how to use a sysvar in Basic Matthew Phillips <spam2011m@yahoo.co.uk> - 2018-09-05 20:19 +0100
Re: how to use a sysvar in Basic Steve Drain <steve@kappa.me.uk> - 2018-09-06 11:19 +0100
Re: how to use a sysvar in Basic Matthew Phillips <spam2011m@yahoo.co.uk> - 2018-09-08 10:32 +0100
Re: how to use a sysvar in Basic Steve Drain <steve@kappa.me.uk> - 2018-09-08 14:56 +0100
Re: how to use a sysvar in Basic Alan Adams <alan@adamshome.org.uk> - 2018-09-08 19:39 +0100
Re: how to use a sysvar in Basic Matthew Phillips <spam2011m@yahoo.co.uk> - 2018-09-10 07:27 +0100
Re: / not (OT) Jim Nagel <jnews18c@abbeypress.co.uk> - 2018-09-10 11:37 +0100
Re: / not (OT) News <chrisjohnson@spamcop.net> - 2018-09-10 14:34 +0100
Re: how to use a sysvar in Basic Alan Adams <alan@adamshome.org.uk> - 2018-09-08 11:20 +0100
Re: how to use a sysvar in Basic Steve Drain <steve@kappa.me.uk> - 2018-09-08 14:40 +0100
Re: how to use a sysvar in Basic "John Williams (News)" <UCEbin@tiscali.co.uk> - 2018-09-04 18:16 +0100
Re: how to use a sysvar in Basic Alan Adams <alan@adamshome.org.uk> - 2018-09-04 22:04 +0100
Re: how to use a sysvar in Basic Matthew Phillips <spam2011m@yahoo.co.uk> - 2018-09-05 08:03 +0100
Re: how to use a sysvar in Basic Martin Wuerthner <spamtrap@mw-software.com> - 2018-09-05 14:39 +0200
Re: how to use a sysvar in Basic Matthew Phillips <spam2011m@yahoo.co.uk> - 2018-09-05 19:45 +0100
Re: how to use a sysvar in Basic Steve Drain <steve@kappa.me.uk> - 2018-09-06 11:24 +0100
Re: how to use a sysvar in Basic "John Williams (News)" <UCEbin@tiscali.co.uk> - 2018-09-05 20:56 +0100
Re: how to use a sysvar in Basic svrsig <chris@svrsig.org> - 2018-09-05 20:13 -0700
Re: how to use a sysvar in Basic Matthew Phillips <spam2011m@yahoo.co.uk> - 2018-09-06 07:21 +0100
Re: how to use a sysvar in Basic A. N. Other <ANOther@argonet.co.uk> - 2018-09-04 18:11 +0100
Re: how to use a sysvar in Basic Jim Nagel <jimnewsm13c@abbeypress.co.uk> - 2018-09-04 18:42 +0100
Re: how to use a sysvar in Basic Matthew Phillips <spam2011m@yahoo.co.uk> - 2018-09-05 08:08 +0100
Re: how to use a sysvar in Basic jgh@mdfs.net - 2018-09-06 17:12 -0700
csiph-web