String heap management

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Hated Moron

Re: String heap management

Post by Hated Moron »

KenDown wrote: Wed 19 Oct 2022, 11:56 The help claims that DIM p% -1 is the equivalent of PRINT HIMEM-END
I hope it doesn't! The statements which are (precisely) equivalent are:

Code: Select all

      DIM p% -1  : REM Set p% to end of heap
      p% = END   : REM Set p% to end of heap
Although of course the returned value is not guaranteed to fit in a 32-bit integer on a 64-bit system, so this should really be:

Code: Select all

      DIM p%% -1 : REM Set p%% to end of heap
      p%% = END  : REM Set p%% to end of heap
but I don't think that can be true, because the DIM statement actually reserves some memory.

Note the statement, "The amount of memory reserved is one byte greater than the value specified. DIM p%% −1 is a special case; it reserves zero bytes of memory." In other words, it reserves -1 bytes +1 byte, which is indeed zero but I suspect that some zeros are worth more than others.
I don't know what you mean by "some zeros are worth more than others". It's easy to see how much memory is 'reserved' by repeating the operation:

Code: Select all

      DIM p%% -1 : PRINT END
      DIM p%% -1 : PRINT END
      DIM p%% -1 : PRINT END
      DIM p%% -1 : PRINT END
      DIM p%% -1 : PRINT END
You should find that the same value is printed each time, confirming that no heap memory is allocated.
Surely if all you want to do is remove trailing spaces, something like this would solve your problem?

Code: Select all

DEFFNtrim(a$):LOCALl%
l%=LENa$
REPEAT:l%-=1:UNTILMID$(a$,l%,1)>" "
=LEFT$(a$,l%)
The easiest (but not necessarily the fastest) way to remove trailing spaces in BBC BASIC is as follows:

Code: Select all

      WHILE RIGHT$(a$) = " " : a$ = LEFT$(a$) : ENDWHILE