Proposal for a 'listlib' library

Discussions related to the code libraries supplied with BB4W & BBCSDL
Richard Russell
Posts: 719
Joined: Tue 18 Jun 2024, 09:32

Re: Proposal for a 'listlib' library

Post by Richard Russell »

tpegc wrote: ↑Mon 05 Oct 2026, 13:58 My thought was "c() is no longer PRIVATE so will be lost at the end of the procedure even if modified within PROC_listcopy"
Yes, but it's not modifying c() it's modifying the pointer to c(), something that is strictly not permitted in BBC BASIC and therefore invokes Undefined Behaviour (whilst you may legitimately read the pointer to an array, the effect of changing it is not documented).

Therefore the only way you can modify the pointer with relative safety is by restoring it to what it originally was, before returning control to BASIC; that way the interpreter ought not to notice what you did! As I said, in the equivalent string case restoring the pointer is essential.

I wouldn't want to give any guarantees as to what might happen if you were running, say, a timer interrupt which by chance happened to access c() whilst its pointer was modified within PROC_listcopy; it might be safe, or it might not.

There is simply no way to emulate Python lists as closely as listlib attempts to, without relying on undocumented behaviour and taking some risks, albeit hopefully small ones. It illustrates the power of BBC BASIC's low-level operations, but also the dangers.