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 - calloc() allocate and zero memory for data elements http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 calloc()            Allocate and zero memory for data elements
------------------------------------------------------------------------------
 Declaration
   memory.hdr

 Syntax
   func _POINTER calloc extern
   param value uint uElements, ;
         value uint uSize

 Arguments
   uElements is the number of elements to allocate.
   uSize is the number of bytes in each element.

 Return
   A pointer to the allocated memory block.

 Description
   The calloc() function tries to allocate memory from the dynamic memory
   heap, to hold the specified number of uElements with the specified
   uSize in bytes.

   If the memory could be allocated, calloc() sets the block with
   zero bytes and 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 returned 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_calloc
   vardef
      ptr( byte ) pData
      ptr( char ) pString
      uint        n
   enddef
   pData := calloc( 100, sizeof( byte ) ) // allocate buffer for 100 bytes
   if pData                            // if allocation succeeded
      pString := pData                 // assign string pointer to same value
      for n := 'a' to 'z'              // place letters in the buffer
         *pData := n
         pData += sizeof( byte )       // increment pointer
      next
      *pData := 0                      // add a zero byte for string display
      ? *pString                       // display contents as string
   else
      ? "Error - cannot allocate memory"
   endif
   free( pData )                       // deallocate buffer
   endproc

   proc main
   Test_calloc()
   endproc

See Also: free() malloc()

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