"Fossies" - the Fresh Open Source Software Archive

Member "FreeBASIC-1.09.0-win64/examples/allocate.bas" (1 Jan 2022, 600 Bytes) of package /windows/misc/FreeBASIC-1.09.0-win64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Visual Basic source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 '' dynamic memory allocation example
    2 
    3 '' Allocate a chunk of memory of a certain size, measured in bytes
    4 dim as integer buffersize = 1024 * 8
    5 dim as byte ptr buffer = allocate( buffersize )
    6 
    7 '' Do something with the buffer
    8 for i as integer = 0 to buffersize - 1
    9     buffer[i] = 123
   10 next
   11 
   12 '' Free up the allocated chunk after we're done using it
   13 deallocate( buffer )
   14 
   15 '' ------------------
   16 
   17 type MyType
   18     x as integer
   19     y as integer
   20 end type
   21 
   22 dim pmytype as MyType ptr
   23 
   24 pmytype = allocate( sizeof( MyType ) )
   25 
   26 pmytype->x = 1234
   27 pmytype->y = 5678
   28 
   29 print pmytype->x, pmytype->y
   30 
   31 deallocate( pmytype )
   32 
   33 sleep