C库函数_malloc calloc & realloc

一. 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)


三. realloc

函数原型:

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).

      





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值