现在的位置: 首页 > 综合 > 正文

malloc

2013年02月06日 ⁄ 综合 ⁄ 共 4055字 ⁄ 字号 评论关闭
MALLOC(3)           Linux Programmers Manual            MALLOC(3)

NAME
       calloc, malloc, free, realloc - 动态分配和回收内存

SYNOPSIS
       #include <stdlib.h>

       void *malloc(size_t size);
       void free(void *ptr);

       void *calloc(size_t nmemb, size_t size);
       void *realloc(void *ptr, size_t size);

DESCRIPTION
       calloc() 为一个具有 nmemb 个元素,每个元素大小为 size 的数组(也可以为结构体类型分配)分配内存.并返回一个指向首元素的指针,同时将初始化该片内存为 0 . 如果 nmemb 或 size 为 0, calloc() 将会要么返回 NULL, 要么返回 一个稍后可以被成功传递给 free() 的唯一指针值。

       malloc()  分配   size 字节 内存 , 并且返回一个指向已分配内存的指针. 该内存并不会被初始化. 如果  size 为 0 ,则将会要么返回 NULL, 要么返回 一个稍后可以被成功传递给 free() 的唯一指针值。

       free() 回收被指针 ptr 指向的内存空间, 并且 ptr 必须是在此之前调用 malloc(), calloc() 或 realloc() 返回所得到的.  此外 如果 free(ptr) 已经被回收过一次,则再次调用 free(ptr) 来进行回收,将会发生不可预料的后果,如果此时 ptr 为 NULL,则不会产生任何操作.

       realloc() 修改 指针 ptr 指出的内存空间为 size 字节. 并且之前所指和之后所指的交集部分中的内容不会改变。且新分配的部分不会被初始化; .  若 ptr 为  NULL,该调用等同于调用 malloc(size); 若 size 为 zero,
该调用等同于调用 free(ptr).  除非 ptr 为 NULL, 否则 ptr 必须是在此之前调用 malloc(), calloc() 或 realloc() 返回所得到的.若 ptr 所指的内存空间已经被移动到其他地方,则此时的调用等同于调用 free(ptr).

RETURN VALUE
       对于 calloc() 和 malloc(), 若请求分配成功,返回值是一个指向分配区域的指针,并且该指针可以合适的于、与任意类型的变量对齐,  若请求失败,则返回 NULL
       free() 函数 无返回值.

       realloc()  返回一个指向新分配内存区域的指针,的指针,并且该指针可以合适的于、与任意类型的变量对齐,
       并且 该指针 可能与 ptr 不同, 或者在请求失败时候返回 NULL . 若 size 为 0, 要么 NULL 指针被返回
       要么返回一个可以传递给 free() 的适当的指针被返回. 如果 realloc() 调用失败 ,则原来的内存块将不会被改变。

CONFORMING TO
       C89, C99.

NOTES
       通常, malloc() 从 heap 分配内存, 并且必须通过调节 heap 的大小来获得自由空间,与 using sbrk(2)不同.    When allocating blocks of mem
       ory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation
       allocates the memory as a  private  anonymous  mapping  using  mmap(2).
       MMAP_THRESHOLD  is  128    kB  by    default,  but is adjustable using mal
       lopt(3).  Allocations performed using mmap(2)  are  unaffected  by  the
       RLIMIT_DATA resource limit (see getrlimit(2)).

       The  Unix98  standard requires malloc(), calloc(), and realloc() to set
       errno to ENOMEM upon failure.  Glibc assumes that this is done (and the
       glibc  versions of these routines do this); if you use a private malloc
       implementation that does not set errno, then certain  library  routines
       may fail without having a reason in errno.

       Crashes    in  malloc(), calloc(), realloc(), or free() are almost always
       related to heap corruption, such as overflowing an allocated  chunk  or
       freeing the same pointer twice.

       Recent  versions  of  Linux  libc  (later  than 5.4.23) and glibc (2.x)
       include a malloc() implementation  which  is  tunable  via  environment
       variables.   When  MALLOC_CHECK_  is  set,  a  special (less efficient)
       implementation is used which is designed to be tolerant against    simple
       errors, such as double calls of free() with the same argument, or over
       runs of a single byte (off-by-one bugs).  Not all such  errors  can  be
       protected  against,  however,  and  memory  leaks  can result.  If MAL
       LOC_CHECK_ is set to  0,  any  detected    heap  corruption  is  silently
       ignored; if set to 1, a diagnostic message is printed on stderr; if set
       to 2, abort(3) is called immediately; if set to 3, a diagnostic message
       is  printed on stderr and the program is aborted.  Using a nonzero MAL
       LOC_CHECK_ value can be useful because otherwise  a  crash  may    happen
       much  later,  and  the  true cause for the problem is then very hard to
       track down.

BUGS
       By default, Linux follows an  optimistic  memory  allocation  strategy.
       This  means  that  when malloc() returns non-NULL there is no guarantee
       that the memory really is available.  This is a    really    bad  bug.   In
       case  it  turns    out that the system is out of memory, one or more pro
       cesses will be killed by the infamous OOM killer.   In  case  Linux  is
       employed  under    circumstances where it would be less desirable to sud
       denly lose some randomly picked processes, and moreover the kernel ver
       sion  is  sufficiently  recent,    one can switch off this overcommitting
       behavior using a command like:

       # echo 2 > /proc/sys/vm/overcommit_memory

       See also  the  kernel  Documentation  directory,  files    vm/overcommit-
       accounting and sysctl/vm.txt.

SEE ALSO
       brk(2), mmap(2), alloca(3), posix_memalign(3)

COLOPHON
       This  page  is  part of release 2.77 of the Linux man-pages project.  A
       description of the project, and information about reporting  bugs,  can
       be found at http://www.kernel.org/doc/man-pages/.

GNU                  2007-09-15                 MALLOC(3)

抱歉!评论已关闭.