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)は、sizeバイト(byte)のメモリ領域をスタック(heap)から動的に割り当て、割り当てられたメモリ領域のヘッダアドレスを指すポインタを返します.割り当てられたメモリ領域は初期化およびクリーンアップされていません.
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()またはrealoc()によってスタック上の動的メモリから割り当てられている必要があります.
そうでない場合、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()、reallocated 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]#