C语言中内存分配和释放

这里主要测试了与内存分配和释放有关的几个函数,这几个函数的说明如下:

Memory Functions

calloc

Declaration:

void *calloc(size_t  nitems , size_t  size );
Allocates the requested memory and returns a pointer to it. The requested size is  nitems each  size bytes long (total memory requested is nitems*size). The space is initialized to all zero bits.

On success a pointer to the requested space is returned. On failure a null pointer is returned.

free

Declaration:

void free(void * ptr );
Deallocates the memory previously allocated by a call to  callocmalloc, or  realloc. The argument  ptr points to the space that was previously allocated. If  ptr points to a memory block that was not allocated with  callocmalloc, or  realloc, or is a space that has been deallocated, then the result is undefined.

No value is returned.

malloc

Declaration:

void *malloc(size_t  size );
Allocates the requested memory and returns a pointer to it. The requested size is  size bytes. The value of the space is indeterminate.

On success a pointer to the requested space is returned. On failure a null pointer is returned.

realloc

Declaration:

void *realloc(void * ptr , size_t  size );
Attempts to resize the memory block pointed to by  ptr that was previously allocated with a call to  malloc or  calloc. The contents pointed to by  ptr are unchanged. If the value of  size is greater than the previous size of the block, then the additional bytes have an undeterminate value. If the value of  size is less than the previous size of the block, then the difference of bytes at the end of the block are freed. If ptr is null, then it behaves like  malloc. If  ptr points to a memory block that was not allocated with calloc or  malloc, or is a space that has been deallocated, then the result is undefined. If the new space cannot be allocated, then the contents pointed to by  ptr are unchanged. If size is zero, then the memory block is completely freed.

On success a pointer to the memory block is returned (which may be in a different location as before). On failure or if size is zero, a null pointer is returned.

 

#include < stdio.h >
#include
< stdlib.h >
#include
< string .h >

 
/*
  * void *calloc(size_t nitems, size_t size)
  *     为具体类型分配内存时需要强制类型转化,将(void *)转化成其他类型。
  *     calloc()用来配置nmemb个相邻的内存单位,每一单位的大小为size,并返回指向第一个元素的指针。  
  *     这和使用下列的方式效果相同:malloc(nmemb*size);不过,在利用calloc()配置内存时会将内存内容初始化为0。
  *     若配置成功则返回一指针,失败则返回NULL。

  * void free(void *ptr)
  *     参数ptr为指向先前由malloc()、calloc()或realloc()所返回的内存指针。调用free()后ptr所指的内存空间便会被收回。
  *     假若参数ptr所指的内存空间已被收回或是未知的内存地址,则调用free()可能会有无法预期的情况发生。若参数ptr为NULL,则free()不会有任何作用。
  *     一般要在free(ptr);后写如下语句ptr=NULL;防止ptr编程野指针。

  * size_t getpagesize(void) 
  *     返回一分页的大小,单位为字节(byte)。此为系统的分页大小,不一定会和硬件分页大小相同。
  *     内存分页大小。附加说明在Intel x86 上其返回值应为4096bytes。
  *     在头文件<unistd.h>中,而<unistd.h>在linux C中定义的,在windows c中则没有定义
  *

  * void * malloc(size_t size)
  *     malloc()用来配置内存空间,其大小由指定的size决定。若配置成功则返回一指针,失败则返回NULL。
  
  * void *realloc(void *ptr, size_t size)
  *     这里需要注意的是,一般使用这函数是为了增加内存,该情况下原来指针指向内容不会变,新增的内存的值则不确定。
  *     Attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc. 
  *     The contents pointed to by ptr are unchanged. If the value of size is greater than the previous size of the block, 
  *     then the additional bytes have an undeterminate value. If the value of size is less than the previous size of the block, 
  *     then the difference of bytes at the end of the block are freed. If ptr is null, then it behaves like malloc. 
  *     If ptr points to a memory block that was not allocated with calloc or malloc, or is a space that has been deallocated, then the result is undefined. 
  *     If the new space cannot be allocated, then the contents pointed to by ptr are unchanged. If size is zero, then the memory block is completely freed.
  
*/

struct  student
{
    
int  age;
    
char  name[ 20 ];
};

int  main()
{
    
int  i  =   0 ;

    
struct  student  * Lily  =  ( struct  student  * )calloc( 10 , sizeof ( struct  student));

    
if (Lily != NULL)
    {
        Lily[
0 ].age  =   10 ;
        strcpy(Lily[
0 ].name, " Lily " );
    }
    
else
    {
        printf(
" calloc failure! " );
    }


    
for (i  =   0  ; i  <   10 ; i ++ )
    {
        printf(
" Lily[%d]'s name:%s ; Lily[%d]'s age:%d\n " ,i,Lily[i].name,i,Lily[i].age);
    }

    system(
" pause " );
    
return   0 ;
}

 

 

转载于:https://www.cnblogs.com/zxher/archive/2010/06/25/1765209.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值