"Fossies" - the Fresh Open Source Software Archive

Member "FreeBASIC-1.09.0-win64/examples/manual/memory/allocate2.bas" (1 Jan 2022, 726 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 '' examples/manual/memory/allocate2.bas
    2 ''
    3 '' NOTICE: This file is part of the FreeBASIC Compiler package and can't
    4 ''         be included in other distributions without authorization.
    5 ''
    6 '' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgAllocate
    7 '' --------
    8 
    9 '' Bad example of Allocate usage, causing memory leaks
   10 
   11 Sub BadAllocateExample()
   12 
   13     Dim p As Byte Ptr
   14 
   15     p = Allocate(420)   '' assign pointer to new memory
   16 
   17     p = Allocate(420)   '' reassign same pointer to different memory,
   18                         '' old address is lost and that memory is leaked
   19 
   20     Deallocate(p)
   21 
   22 End Sub
   23 
   24     '' Main
   25     BadAllocateExample() '' Creates a memory leak 
   26     Print "Memory leak!"
   27     BadAllocateExample() '' ... and another
   28     Print "Memory leak!"
   29     End