Next Previous Contents

2. Package for allocating memory with fixing some allocation errors

Developing modern compilers it is necessary to avoid arbitrary limits on the length or number of any data structure by allocating all data structures dynamically. Here the package `allocate' which implements allocation and freeing memory with automatic fixing allocation errors is suggested.

The package automatically calls action on situation `no memory'. The action never returns control back because after calling function which processes allocation error the function `abort' is always called. Therefore the function which processes allocation error should not return control back.

The interface part of the package is file `allocate.h'. The implementation part is file `allocate.cpp'. The interface contains class `allocate' without any public constructors/destructors. The class has the following functions:

Static public function `change_error_function'

        `void change_error_function
              (void (*error_function) (void))) (void)'
        
is used for changing up action on the situation `no memory'. The function also returns former function which was early action on the situation `no memory'.

Static public function `default_error_function'

        `void default_error_function (void)'
        
is default action of the package on the situation `no memory'. This action consists of output message `*** no memory ***' to standard error stream and calling function `exit' with code equals to 1.

Static public function `malloc'

        `void *malloc (size_t size)'
        
is analogous to ANSI C library function `malloc'. The parameter is size of needed memory. The function returns address of allocated memory.

Static public function `calloc'

        `void *calloc (size_t nel, size_t size)'
        
is analogous to ANSI C library function `calloc'. The parameters have the same sense as in standard function `calloc'. The function returns address of allocated memory.

Static public function `free'

        `void free (void *ptr)'
        
is analogous to ANSI C library function `free' but can accept nil pointer value. In this case function does nothing.

Static public function `realloc'

        `void *realloc(void *old, size_t size)'
        
is analogous to ANSI C library function `realloc'. The first parameter is old address of reallocated memory. The second parameter is new size of reallocated memory. The function returns new address of reallocated memory.


Next Previous Contents