一. malloc
函数原型:
void* malloc( size_t size ); //Defined in header <stdlib.h> 返回任意类型值地址,可以强转
Parameters:
size -- number of bytes to allocate
Return value:
Pointer to the beginning of newly allocated memory or null pointer if error has occurred.
The pointer must be deallocated with free().
注解:
1. if allocation succeeds, returns a pointer to the lowest (first) byte in the allocated memory block that is suitably aligned for any object type
2. malloc
is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage
3. If size
is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be
returned that may not be used to access storage)
二. calloc
函数原型:
void* calloc( size_t num, size_t size );
Parameters:
num -- number of objects
size -- size of each objects
Return value:
与 malloc 一样
注解:
1. Allocates memory for an array of num
objects of size size
and initializes all bits in the allocated storage to zero
2. If allocation succeeds, returns a pointer to the lowest (first) byte in the allocated memory block that is suitably aligned for any object type.
3. If size
is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage)
函数原型:
void *realloc( void *ptr, size_t new_size );
Parameters:
ptr -- pointer to the memory area to be allocated
new_size -- new size of the array
Return value :
同上
注解:
1. Reallocates the given area of memory. It must be previously allocated by malloc(), calloc() or realloc()
and not yet freed with free, otherwise, the results are undefined.
2. f there is not enough memory, the old memory block is not freed and null-pointer is returned
3. If ptr
is NULL, the behavior is the same as calling malloc(new_size
).