What's the D way of allocating memory?

Gary Willoughby dev at nomad.so
Thu Aug 8 11:35:04 PDT 2013


On Wednesday, 7 August 2013 at 22:03:16 UTC, Ali Çehreli wrote:
> qalloc seems better but just like in C and C++, when comparing 
> GC.malloc and GC.calloc, the default choice should always be 
> GC.calloc.

As a side note, i was just checking this out in core.memory and i 
was surprised:

     /**
      * Requests an aligned block of managed memory from the 
garbage collector,
      * which is initialized with all bits set to zero.  This 
memory may be
      * deleted at will with a call to free, or it may be 
discarded and cleaned
      * up automatically during a collection run.  If allocation 
fails, this
      * function will call onOutOfMemory which is expected to 
throw an
      * OutOfMemoryError.
      *
      * Params:
      *  sz = The desired allocation size in bytes.
      *  ba = A bitmask of the attributes to set on this block.
      *
      * Returns:
      *  A reference to the allocated memory or null if 
insufficient memory
      *  is available.
      *
      * Throws:
      *  OutOfMemoryError on allocation failure.
      */
     static void* calloc( size_t sz, uint ba = 0 ) pure nothrow
     {
         return gc_calloc( sz, ba );
     }

calloc is marked as nothrow but apparently throws 
OutOfMemoryError on allocation failure? ..eh? O_o


More information about the Digitalmars-d-learn mailing list