Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Force 4.0 Reference - malloc() allocate dynamic memory http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 malloc()            Allocate dynamic memory
------------------------------------------------------------------------------
 Declaration
   memory.hdr

 Syntax
   func _POINTER malloc extern
   param value uint uSize

 Arguments
   uSize is the number of bytes to allocate.

 Return
   A pointer to the allocated memory block.

 Description
   The malloc() function tries to allocate memory in dynamic memory
   heap, of the specified uSize.

   If the memory could be allocated, malloc() returns a pointer to the
   start of the memory block, otherwise it returns 0. Before accessing
   dynamic memory, you must check if it could be allocated by testing if
   the returned pointer is non-zero.

   The pointer is needed for later access to the memory block, and to release
   memory by calling the free() procedure.

 Example
   #define EXAMPLE_MEMORY
   #include example.hdr

   proc Test_malloc
   vardef
      ptr( byte ) pMem[ 11 ]
      uint        n
   enddef
   ? 0, coreleft()
   for n := 1 to 10
      pMem[ n ] := malloc( 1024 )      // allocate memory
      ? n, coreleft()                  // display amount of free memory
   next
   ?
   ? 10, coreleft()
   for n := 10 downto 1  // allocations must be released in reverse order
      free( pMem[ n ] )                // free memory
      ? n - 1, coreleft()              // display amount of free memory
   next
   endproc

   proc main
   Test_malloc()
   endproc

See Also: calloc() free()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson