📄 malloc.3
字号:
.\" SCCSID: @(#)malloc.3 8.1 9/11/90.TH malloc 3 VAX.SH Namemalloc, free, realloc, calloc, alloca \- memory allocator.SH Syntax.nf.B #include <stdlib.h>.PP.B void *malloc(\fIsize\fP).B size_t \fIsize\fP; .PP.B free(\fIptr\fP).B void *\fIptr\fP;.PP.B void *realloc(\fIptr, size\fP) .B void *\fIptr\fP;.B size_t \fIsize\fP; .PP.B void *calloc(\fInelem, elsize\fP) .B size_t \fInelem, elsize\fP;.PP.B void *alloca(\fIsize\fP).B size_t \fIsize\fP;.fi.SH Description.NXR "malloc subroutine (standard C)".NXR "free subroutine (standard C)".NXR "realloc subroutine (standard C)".NXR "calloc subroutine (standard C)".NXR "alloca subroutine".NXR "memory" "shared memory".NXR "memory" "allocating"The.PN mallocand.PN freesubroutinesprovide a simple general-purpose memory allocation package.The.PN mallocsubroutinereturns a pointer to a block of at least.I sizebytes beginning on a word boundary..PPThe argument to.PN freeis a pointer to a block previously allocated by.PN malloc .This space is made available for further allocation,but its contents are left undisturbed..PPNeedless to say, grave disorder will result if the space assigned by.PN mallocis overrun or if some random number is handed to.PN free ..PPThe.PN mallocsubroutinemaintains multiple lists of free blocks according to size,allocating space from the appropriate list.It calls.PN sbrkto get more memory from the system when there is nosuitable space already free.For further information, see .MS brk 2 ..PPThe.PN reallocsubroutine changes the size of the block pointed to by.I ptrto.I sizebytes and returns a pointer to the (possibly moved) block.The contents will be unchanged up to thelesser of the new and old sizes..PPIf \fIptr\fP is a null pointer, then.PN reallocbehaves like.PN malloc for the specified \fIsize\fP. If \fIsize\fP is zero, then.PN reallocfrees the space pointed to by \fIptr\fP..PPIn order to be compatible with older versions,.PN reallocalso works if.I ptrpoints to a block freed since the last call of.PN malloc ,.PN realloc ,or.PN calloc .Sequences of.PN free ,.PN malloc, and.PN reallocwere previously used to attempt storage compaction.This procedure is no longer recommended..PPThe.PN callocsubroutine allocates space for an array of.I nelemelements of size.I elsize.The space is initialized to zeros..PPThe.PN allocasubroutine allocates .I sizebytes of space in the stack frame of the caller.This temporary space is automatically freed onreturn..PPEach of the allocation routines returns a pointerto space suitably aligned (after possible pointer coercion)for storage of any type of object..SH Restrictions.NXR "malloc subroutine (standard C)" "restrictions"When.PN reallocreturns 0, the block pointed to by.I ptrmay be destroyed..PPCurrently, the allocator is unsuitable for direct use in a largevirtual environment where many small blocks are kept, since it keeps all allocated and freed blocks on a circularlist.Just before more memory is allocated,all allocated and freed blocks are referenced..PPThe.PN allocasubroutine is machine dependent..SH DiagnosticsThe.PN malloc ,.PN realloc ,and.PN callocsubroutinesreturn a nullpointer (0) if there is no available memory or if the arenahas been detectably corrupted by storing outside the bounds of a block..PPThe.PN malloc ,.PN realloc ,.PN calloc ,and.PN allocasubroutines will fail and no additional memory will be allocated ifone of the following is true:.TP 12[ENOMEM]The limit, as set by.MS setrlimit 2 ,is exceeded..TP[ENOMEM]The maximum possible size of a data segment (compiled into thesystem) is exceeded..TP[ENOMEM]Insufficient space exists in the swap area to support the expansion.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -