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 withcalloc 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;
}

 

 

posted on 2010-06-25 15:18  虚怀若谷  阅读(2326)  评论(0编辑  收藏  举报

导航