Memory Management

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
kirkkaf13@gmail.com
Posts: 9
Joined: Fri 23 Aug 2024, 21:37

Memory Management

Post by kirkkaf13@gmail.com »

Hi Everyone,

I saw a recent announcement for BB4W and wanted to play around with it since my job no longer requires me to actively write software and when I did it was a lot of JavaScript so memory management wasn't really anything we thought about too much.

Apologies in advance for what might seem like basic questions I am still trying to absorb how BB4W stores variables in memory using linked lists. I have an interest in graphics and more specifically DirectX9 so thought I'd try and understand more as a hobby.

I was looking at the D3D9LIB.bbc and trying to get an understanding of how it works and came across the following syntax:

Code: Select all

local S%
S%!24=1
S%!28=H%
S%!32=1
So first of all the variable names in all these examples seem to just be letters, I am assuming this has something to do with what I read on how BB4W stores variables?

As for the code above I think I know what is happening, we have declared a local variable with 4 bytes. This is used as a pointer and we are shifting by 24 bytes and storing an integer 1 moving another 4 bytes and storing another integer ect. So S% is like a pointer. How can we be sure that the memory 24 bytes from S% hasn't already been used for another value by BB4W?

Wouldn't a struct of be a better or safer way to do this?

Thanks
Richard Russell
Posts: 366
Joined: Tue 18 Jun 2024, 09:32

Re: Memory Management

Post by Richard Russell »

kirkkaf13@gmail.com wrote: Thu 03 Apr 2025, 10:52 Wouldn't a struct be a better or safer way to do this?
D3D9LIB is a very old library, it's even possible that it was written before structures were introduced into BBC BASIC!

Another possible explanation is that libraries are written for performance, good coding practice is set aside if by so doing execution speed is increased, and indirection probably is faster than structures.

You'll see all sorts of 'horrors' in libraries for that reason, but as they are supposed to be treated as 'black boxes' code quality is of relatively little importance there.

More generally, neither libraries nor example programs are (as a rule) updated to take advantage of new language features, so you'll often find anachronistic code.

Only yesterday I was looking at the code of bigint.bbc and was annoyed by the (now) unnecessary checking it does of whether it's running on a 32 or 64 bit platform.

In a perfect world one would have a 'blitz' of all the libraries and example programs, to update them to take advantage of the latest improvements to the language, but it's very unlikely ever to happen.
User avatar
JeremyNicoll
Posts: 73
Joined: Sun 26 Jul 2020, 22:22
Location: Edinburgh

Re: Memory Management

Post by JeremyNicoll »

kirkkaf13@gmail.com wrote: Thu 03 Apr 2025, 10:52 I am still trying to absorb how BB4W stores variables in memory using linked lists.
Not all of them (as far as I know). The static integer variables A% thru Z% are, I think, elsewhere since (especially in early versions of BASIC there were possibly only those integer variables available), but in any case having a handful of variables which have space pre-allocated for them makes code that uses them run faster. Presumably the interpreter checks if they're in use before searching along the linked lists.

See: https://www.bbcbasic.co.uk/bbcwin/manua ... tml#static


kirkkaf13@gmail.com wrote: Thu 03 Apr 2025, 10:52 I was looking at the D3D9LIB.bbc and trying to get an understanding of how it works and came across the following syntax:

Code: Select all

local S%
S%!24=1
S%!28=H%
S%!32=1
As for the code above I think I know what is happening, we have declared a local variable with 4 bytes....
You missed out the fact that there's DIM (or maybe DIM LOCAL) statement which specifies the length of the reserved storage which S% points to. See the manual section on DIM:

https://www.bbcbasic.co.uk/bbcwin/manua ... 4.html#dim

especially the "Reserving a temporary area of memory" section.
Richard Russell
Posts: 366
Joined: Tue 18 Jun 2024, 09:32

Re: Memory Management

Post by Richard Russell »

JeremyNicoll wrote: Sat 05 Apr 2025, 18:42
kirkkaf13@gmail.com wrote: Thu 03 Apr 2025, 10:52 I am still trying to absorb how BB4W stores variables in memory using linked lists.
Not all of them (as far as I know).
The OP doesn't say that all variables are stored using linked lists, but that "BB4W stores variables in memory using linked lists" which is perfectly true, it does. The memory layout of those lists is described in the documentation here. Indeed in that very documentation it states that "Variables are held within memory as linked lists (chains)", it doesn't feel the need to mention the exceptions! :lol: