User Tools

Site Tools


allocating_and_freeing_memory_blocks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
allocating_and_freeing_memory_blocks [2020/06/08 15:46] – created richardrussellallocating_and_freeing_memory_blocks [2024/01/05 00:22] (current) – external edit 127.0.0.1
Line 40: Line 40:
  
 These routines may not be as efficient or fast as the OS equivalents, and they use up space in BASIC's heap which the OS allocation functions don't, but nevertheless there may be occasions when they are useful. These routines may not be as efficient or fast as the OS equivalents, and they use up space in BASIC's heap which the OS allocation functions don't, but nevertheless there may be occasions when they are useful.
 +
 +//January 2021//
 +
 +When this article was originally published I didn't include an equivalent of the **realloc()** functionality, such as can be achieved in BBCSDL using **SYS "SDL_realloc"**.  To rectify that omission here is a function which can re-size a block previously allocated with **FN_heapalloc()**:
 +
 +<code bb4w>
 +      DEF FN_heaprealloc(p%%, N%)
 +      LOCAL a$ : N% += 23
 +      SWAP ]^a$, ](p%%-8)
 +      p%% -= PTR(a$)
 +      IF N% < LENa$ a$ = LEFT$(a$,N%)
 +      IF N% > LENa$ a$ += STRING$(N% - LENa$, CHR$0)
 +      p%% += PTR(a$)
 +      SWAP ](p%%-8), ]^a$
 +      = p%%
 +</code>
 +
 +Please note that whilst **FN_heapalloc()** always returns a paragraph-aligned block of memory, **FN_heaprealloc()** doesn't.  If it's essential that the memory be aligned you should create a new block of the wanted size, copy the contents of the old block into it, and free the old block.
allocating_and_freeing_memory_blocks.1591631199.txt.gz · Last modified: 2024/01/05 00:18 (external edit)