calloc, malloc, free, realloc - Allocate and free dynamic memory

1. calloc:   void *calloc(size_t nmemb, size_t size);

         calloc()  allocates memory for an array of nmemb elements of size bytes  each and returns a pointer to the allocated memory.  The memory is se to zero.

     函数:calloc(nmemb, size) 分配连续的大小为nmemb*size个字节(byte)内存空间, 返回一个指向所分配的内存空间的首地址的指针,并且所有的内存单元都被设置为0.

      分析:由于要把所有分配的内存单元都要初始化为0,其速度相比malloc(),要慢,由于malloc分配的内存并不初始化。


2. malloc: void *malloc(size_t size);

       malloc()  allocates  size  bytes and returns a pointer to the allocated memory.  The memory is not cleared.

      函数:malloc(size)  从堆(heap)上动态分配 size个字节(byte)的内存空间,并返回一个指向所分配的内存空间的首地址的指针。所分配的内存空间没有被初始化和清理。


3. free: void free(void *ptr);

                free() frees the memory space pointed to by ptr, which must  have  been returned by a previous call to malloc(), calloc() or realloc().  

               Other- wise, or  if  free(ptr)  has  already  been  called  before,  undefined behaviour occurs.  If ptr is NULL, no operation is performed.

        函数: free() 函数将 指针ptr指向的内存空间释放,但ptr指向的内存空间必须是由malloc()或者calloc()或者realloc() 从堆上的动态内存,已分配的。

                   否则,  如果free(ptr) 在此之前 已经被调用, 将产生一个未定义的行为。  

                                 如果ptr==NULL, free(ptr),  不执行任何操作。

  可见,释放动态内存的一个好习惯:

if(NULL != ptr) {
	free(ptr);
	ptr = NULL;
}

4. realloc:  void *realloc(void *ptr, size_t size);
     realloc()  changes  the  size  of the memory block pointed to by ptr to size bytes. 
    The contents will be unchanged to the minimum of  the  old and new sizes; newly allocated memory will be uninitialized.  
     If ptr is  NULL, the call is equivalent to malloc(size); if size is equal to zero,  the  call is equivalent to free(ptr). 
     Unless ptr is NULL, it must have been returned by an earlier call to malloc(),  calloc()  or  realloc(). 
     If the area pointed to was moved, a free(ptr) is done.
   函数: realloc(ptr, size)  将ptr指向的内存块的大小重分配为size个字节(byte)。 (可见,可以使ptr指向的内存块增大,也可以是ptr指向的内存块减小)。
               内存块中的内容在size比原来的size小时,内容保持不变;  当新size比原来的size大时,原来size大小的内存的内容保持不变,并且新分配的内存没有初始化。
               如果ptr==NULL, realloc(ptr, size)功能 等价于malloc(size);  若果size ==0 , realloc(ptr, size) 功能等价于 free(ptr).
              若ptr != NULL,  ptr  必须是前一个malloc() 或者 calloc()或者 realloc() 函数返回的指针。
             如果内存空间在重分配的过程中发生了移动,  则期间,free(ptr) 被调用,即ptr指向的内存空间清理掉了。


5. calloc(), malloc(), free(), realloc() 的返回值:
RETURN VALUE
       For calloc() and malloc(),  the value returned is a pointer to the allocated memory, 
        which is suitably aligned for any kind  of  variable,  or  NULL if the request fails.
对于函数calloc() 和malloc(), 其返回值是一个指向所分配内存空间的首地址的指针,该指针对于任何类型的变量进行适当的对齐。
       否则,分配失败时, NULL 作为返回值。 


       free() returns no value.  
      free()函数不返回任何值。




       realloc()  returns  a  pointer  to the newly allocated memory,  which is  suitably aligned for any kind of variable and  may  be  different  from  ptr, or NULL if the request fails.  
If size was equal to 0, either NULL or a pointer suitable to be passed to free() is returned. 
If realloc()  fails the original block is left untouched; it is not freed or moved.
函数realloc() , 在动态内存成功分配时,返回一个指向新分配的内存块的首地址的指针,该指针适用于任何类型变量的对齐,而且其可能与ptr不同;否则,失败时,返回一个 NULL.
若果 size==0,    或者返回NULL, 或者返回一个适合传递给free()函数的指针被返回。
若果realloc(ptr, size)函数动态内存分配失败,ptr指向的原有的内存块保持不变,即原来的内存空间,既没有被释放也没有被移动。

 

 

6. Linux manual 中的解释如下:

[root@wzb test]# man realloc | cat
MALLOC(3)                  Linux Programmer’s Manual                 MALLOC(3)



NAME
       calloc, malloc, free, realloc - Allocate and free dynamic memory

SYNOPSIS
       #include <stdlib.h>

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

DESCRIPTION
       calloc()  allocates memory for an array of nmemb elements of size bytes
       each and returns a pointer to the allocated memory.  The memory is  set
       to zero.

       malloc()  allocates  size  bytes and returns a pointer to the allocated
       memory.  The memory is not cleared.

       free() frees the memory space pointed to by ptr, which must  have  been
       returned by a previous call to malloc(), calloc() or realloc().  Other-
       wise, or  if  free(ptr)  has  already  been  called  before,  undefined
       behaviour occurs.  If ptr is NULL, no operation is performed.

       realloc()  changes  the  size  of the memory block pointed to by ptr to
       size bytes.  The contents will be unchanged to the minimum of  the  old
       and new sizes; newly allocated memory will be uninitialized.  If ptr is
       NULL, the call is equivalent to malloc(size); if size is equal to zero,
       the  call is equivalent to free(ptr).  Unless ptr is NULL, it must have
       been returned by an earlier call to malloc(),  calloc()  or  realloc().
       If the area pointed to was moved, a free(ptr) is done.

RETURN VALUE
       For calloc() and malloc(), the value returned is a pointer to the allo-
       cated memory, which is suitably aligned for any kind  of  variable,  or
       NULL if the request fails.

       free() returns no value.

       realloc()  returns  a  pointer  to the newly allocated memory, which is
       suitably aligned for any kind of variable and  may  be  different  from
       ptr, or NULL if the request fails.  If size was equal to 0, either NULL
       or a pointer suitable to be passed to free() is returned.  If realloc()
       fails the original block is left untouched; it is not freed or moved.

CONFORMING TO
       C89, C99.

SEE ALSO
       brk(2), posix_memalign(3)

NOTES
       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(), free() or realloc() 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 GNU libc (2.x)
       include a malloc implementation which is tunable via environment  vari-
       ables.  When MALLOC_CHECK_ is set, a special (less efficient) implemen-
       tation is used which is designed to be tolerant against simple  errors,
       such as double calls of free() with the same argument, or overruns of a
       single byte (off-by-one bugs).  Not all such errors  can  be  protected
       against, however, and memory leaks can result.  If MALLOC_CHECK_ is set
       to 0, any detected heap corruption is silently  ignored  and  an  error
       message  is not generated; if set to 1, the error message is printed on
       stderr, but the program is not aborted; if set to 2, abort() is  called
       immediately,  but  the error message is not generated; if set to 3, the
       error message is printed on stderr and program is aborted.  This 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 processes
       will be killed by the infamous OOM killer.  In case Linux  is  employed
       under  circumstances  where it would be less desirable to suddenly lose
       some randomly picked processes, and moreover the kernel version is suf-
       ficiently 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.



GNU                               1993-04-04                         MALLOC(3)
[root@wzb test]#
                             


 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值