|
Memory Allocation Library Functions | bsdmalloc(3MALLOC) |
| bsdmalloc - memory allocator |
SYNOPSIS
|
cc [ flag ... ] file ... -lbsdmalloc [ library ... ] char *malloc(size, unsigned size;
|
| int free( ptr, char *ptr;
|
| char *realloc( ptr, size, char *ptr;
unsigned size;
|
|
These routines provide a general-purpose memory allocation
package. They maintain a table of free blocks for efficient allocation and coalescing of free storage. When there is no suitable space already free, the allocation routines call sbrk(2)
to get more memory from the system. Each of the allocation routines returns a pointer to space suitably aligned for storage of any type of object. Each returns a null pointer if the request cannot be completed.
The malloc() function returns a pointer to a block of at least size bytes, which is appropriately aligned.
The free() function releases a previously allocated block. Its argument is a pointer to a block previously allocated by malloc() or realloc().
The realloc() function changes the size of the block referenced by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. If unable to
honor a reallocation request, realloc() leaves its first argument unaltered. For backwards compatibility, realloc() accepts a pointer to a block freed since the most recent call to malloc() or realloc().
|
|
The malloc() and realloc() functions return a null pointer if there is not enough available memory. When realloc() returns NULL, the block pointed to by ptr is left intact.
|
|
If malloc() or realloc() returns unsuccessfully, errno will be set to indicate the following:
-
ENOMEM
-
size bytes of memory cannot be allocated because it exceeds the physical limits of the system.
-
EAGAIN
- There is not enough memory available at this point in time to allocate size bytes of memory; but the application could try again later.
|
|
Using realloc() with a block freed before the most recent call to malloc() or realloc() results in an error.
The malloc() and realloc() functions return a non-null pointer if size is 0. These pointers should not be dereferenced.
Always cast the value returned by malloc() and realloc().
Comparative features of bsdmalloc, malloc(3MALLOC), and malloc(3C):
- The bsdmalloc() routines afford better performance but are space-inefficient.
- The malloc(3MALLOC) routines are space-efficient but have slower performance.
- The standard, fully SCD-compliant malloc(3C) routines are a trade-off between performance and space-efficiency.
The free() function does not set errno.
|
| |