Memory Functions: Difference between revisions
Created page with "Memory allocation functions. These functions are utility functions for memory allocation. xmalloc, xrealloc, and xfree are error checking versions of the standard library rou..." |
mNo edit summary |
||
Line 5: | Line 5: | ||
== Contents == | == Contents == | ||
{| border="1" | {| border="1" | ||
!Entity||Type||Scope||Short Description | |||
|- | |- | ||
|xcalloc||function||public||Reserve and initialize storage. | |||
|- | |- | ||
| | |xfree||function||public||Frees a block of storage. | ||
|function | |||
|public | |||
| | |||
| Frees a block of storage. | |||
|- | |- | ||
|xmalloc | |xmalloc||function||public||Reserves a block of storage of size bytes. | ||
|function | |||
|public | |||
| Reserves a block of storage of size bytes. | |||
|- | |- | ||
|xrealloc | |xrealloc||function||public||Changes the size of a previously reserved storage block. | ||
|function | |||
|public | |||
| Changes the size of a previously reserved storage block. | |||
|- | |- | ||
|xstrdup | |xstrdup||function||public||Reserves storage space for a copy of string. | ||
|function | |||
|public | |||
| Reserves storage space for a copy of string. | |||
|} | |} | ||
Line 48: | Line 24: | ||
The storage space to which the return value points is suitably aligned for storage of any type of object. The return value is NULL if there is not enough storage, or if num or size is 0. | The storage space to which the return value points is suitably aligned for storage of any type of object. The return value is NULL if there is not enough storage, or if num or size is 0. | ||
; Source: pm_memory.h:75 | ; Source: pm_memory.h:75 | ||
; Author: Dmitry A.Steklenev | ; Author: Dmitry A. Steklenev | ||
; Version: 1.0 | ; Version: 1.0 | ||
; Code: | ; Code: | ||
public void * xcalloc ( size_t num , | public void * xcalloc ( size_t num , size_t size ) | ||
==function xfree== | ==function xfree== | ||
Frees a block of storage. | Frees a block of storage. | ||
; Source: pm_memory.h:84 | ; Source: pm_memory.h:84 | ||
; Author: Dmitry A.Steklenev | ; Author: Dmitry A. Steklenev | ||
; Version: 1.0 | ; Version: 1.0 | ||
; Code: | ; Code: | ||
Line 69: | Line 42: | ||
Returns a pointer to the reserved space. The storage space to which the return value points is suitably aligned for storage of any type of object. The return value is NULL if size was specified as zero. | Returns a pointer to the reserved space. The storage space to which the return value points is suitably aligned for storage of any type of object. The return value is NULL if size was specified as zero. | ||
; Source: pm_memory.h:42 | ; Source: pm_memory.h:42 | ||
; Author: Dmitry A.Steklenev | ; Author: Dmitry A. Steklenev | ||
; Version: 1.0 | ; Version: 1.0 | ||
; Code: | ; Code: | ||
Line 84: | Line 56: | ||
The storage to which the return value points is aligned for storage of any type of object. | The storage to which the return value points is aligned for storage of any type of object. | ||
; Source: pm_memory.h:59 | ; Source: pm_memory.h:59 | ||
; Author: Dmitry A.Steklenev | ; Author: Dmitry A. Steklenev | ||
; Version: 1.0 | ; Version: 1.0 | ||
; Code: | ; Code: | ||
public void * xrealloc ( void * p , | public void * xrealloc ( void * p, size_t size ) | ||
==function xstrdup== | ==function xstrdup== | ||
Line 96: | Line 66: | ||
Returns a pointer to the storage space containing the copied string. | Returns a pointer to the storage space containing the copied string. | ||
; Source: pm_memory.h:95 | ; Source: pm_memory.h:95 | ||
; Author: Dmitry A.Steklenev | ; Author: Dmitry A. Steklenev | ||
; Version: 1.0 | ; Version: 1.0 | ||
; Code: | ; Code: |
Latest revision as of 02:40, 23 September 2022
Memory allocation functions.
These functions are utility functions for memory allocation. xmalloc, xrealloc, and xfree are error checking versions of the standard library routines malloc, realloc, and free, respectively. They are guaranteed to never return unless there was no problem. Hence, the caller does not need to check for a NULL return value, and the code that calls these functions is simpler due to the lack of error checks.
Contents
Entity | Type | Scope | Short Description |
---|---|---|---|
xcalloc | function | public | Reserve and initialize storage. |
xfree | function | public | Frees a block of storage. |
xmalloc | function | public | Reserves a block of storage of size bytes. |
xrealloc | function | public | Changes the size of a previously reserved storage block. |
xstrdup | function | public | Reserves storage space for a copy of string. |
function xcalloc
Reserve and initialize storage.
Reserves storage space for an array of num elements, each of length size bytes. Then gives all the bits of each element an initial value of 0.
The storage space to which the return value points is suitably aligned for storage of any type of object. The return value is NULL if there is not enough storage, or if num or size is 0.
- Source
- pm_memory.h:75
- Author
- Dmitry A. Steklenev
- Version
- 1.0
- Code
public void * xcalloc ( size_t num , size_t size )
function xfree
Frees a block of storage.
- Source
- pm_memory.h:84
- Author
- Dmitry A. Steklenev
- Version
- 1.0
- Code
public void xfree ( void * p )
function xmalloc
Reserves a block of storage of size bytes.
Returns a pointer to the reserved space. The storage space to which the return value points is suitably aligned for storage of any type of object. The return value is NULL if size was specified as zero.
- Source
- pm_memory.h:42
- Author
- Dmitry A. Steklenev
- Version
- 1.0
- Code
public void * xmalloc ( size_t size )
function xrealloc
Changes the size of a previously reserved storage block.
Returns a pointer to the reallocated storage block. The storage location of the block may be moved by the realloc function. Thus, the p argument to xrealloc is not necessarily the same as the return value.
If size is 0, xrealloc returns NULL.
The storage to which the return value points is aligned for storage of any type of object.
- Source
- pm_memory.h:59
- Author
- Dmitry A. Steklenev
- Version
- 1.0
- Code
public void * xrealloc ( void * p, size_t size )
function xstrdup
Reserves storage space for a copy of string.
Returns a pointer to the storage space containing the copied string.
- Source
- pm_memory.h:95
- Author
- Dmitry A. Steklenev
- Version
- 1.0
- Code
public char * xstrdup ( const char * string )