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 - realloc() reallocate memory block http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 realloc()           Reallocate memory block
------------------------------------------------------------------------------
 Declaration
   memory.hdr

 Syntax
   func _POINTER realloc extern
   param value _POINTER pMem, ;
         value uint     uNewSize

 Arguments
   pMem is a pointer to an allocated memory block.
   uNewSize is the new size of the block in bytes.

 Return
   A pointer to the reallocated block.

 Description
   The realloc() function changes the size of a previously allocated
   memory block. The contents of the block are unchanged up to the
   shorter of the new and old sizes.

 Example
   #define EXAMPLE_MEMORY
   #include example.hdr

   proc Test_realloc
   vardef
      ptr( char( 9 ) ) pMem
      ptr( char( 9 ) ) pTemp
      uint        n
   enddef
   ? "Memory", istr( coreleft() )
   pMem := malloc( 40 )
   if pMem
      ? "Allocated buffer is at  ", dechex( pMem )
      pTemp := pMem
      for n := 1 to 4
         *pTemp := istr( n ) + "  string" // length is 1 + 8 + 1 = 10 bytes
         pTemp += 10
      next
      ? "Memory", istr( coreleft() )
      pMem := realloc( pMem, 80 )
      if pMem
         ? "Reallocated buffer is at", dechex( pMem )
         ? "Memory", istr( coreleft() )
         pTemp := pMem + 40
         for n := 5 to 8
            *pTemp := istr( n ) + "  string" // length is 1 + 8 + 1 = 10 bytes
            pTemp += 10
         next
         pTemp := pMem
         for n := 1 to 8
            ? *pTemp
            pTemp += 10
         next
      endif
      free( pMem )
      ? "Memory", istr( coreleft() )
   endif
   endproc

   proc main
   Test_realloc()
   endproc

See Also: calloc() free() malloc()

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