C语言中内存操作函数

 

一、malloc/calloc

名称:

Malloc/calloc

功能: 

动态内存分配函数

头文件:

#include <stdlib.h>

函数原形:

void *malloc(size_t size);

void *calloc(size_t num,size_t size);

参数: 

size    分配内存块的大小

num  分配内存块的个数

返回值: 

成功返回分配内存块的首地址,失败返回NULL.

malloc和calloc都可以分配内存区,但malloc一次只能申请一个内存区,calloc一次可以申请多个内存区.另外calloc会把分配来的内存区初试化为0,malloc不会进行初始化.

#include <stdio.h>

#include <stdlib.h>

main()

{

    int *p=NULL;

    p=(int *)malloc(sizeof(int));

    if(p==NULL)

    {

        printf("malloc error ");

        exit(1);

    }

    *p=3;

    printf("%d ",*p);

    free(p);

}

 

 

 

二、free

名称:

free

功能: 

动态内存释放函数

头文件:

#include <stdlib.h>

函数原形:

void free(void *ptr);

参数: 

ptr   使用malloc或calloc等内存分配函数所返回的内存指针 

返回值: 

free 可以释放由malloc或calloc等内存分配函数分配的内存.当程序很大时,期间可能要多次动态分配内存,如果不及时释放的话,程序将要占用很大内存.

要注意,如果ptr所指内存已被释放或是未知的内存地址,则可能有无法预期的情况发生.若参数为NULL,则free不会有任何作用.

三、memset

名称:

memset

功能: 

初始化所指定的内存空间

头文件:

#include <stdlib.h>

函数原形:

void *memset(void *buffer,int c,int count);

参数: 

buffer      分配的内存

c      初始化内容

count      初始化的字节数   

返回值: 

返回指向buffer的指针

memset把buffer所指内存区域的前count个字节设置成某个字符的ASCLL值.一般用于给数组,字符串等类型赋值.

main()

{

    int *p=NULL;

    int i;

    char *q=NULL;

    

    p=(int *)malloc(sizeof(int)*10);

    if(p==NULL)

        exit(1);

    memset(p,0,sizeof(int)*10);

    q=p;

    for(i=0;i<10;i++)

        printf("%d",*(q++));

    free(p);

 执行结果是10个0.

四、memcpy

名称:

memcpy

功能: 

拷贝内存空间

头文件:

#include <stdlib.h>

函数原形:

void *memcpy(void *dest,void *src,unsigned int count);

参数: 

dest       目标内存区

src    原内存区

count      要复制的字节数

返回值: 

指向dest的指针

memcpy会把src所指内存区复制count个字节到dest所指内存区.如果count比src字节数大,strcpy会拷贝''后结束.要注意dest和src不要重叠.

memcpy只是拷贝内存空间,不处理空间重叠的问题.

main()

{

    int *p1=NULL;

    int *p2=NULL;

  int q;

    int i;

    

    p1=malloc(sizeof(int)*10);

    if(p1==NULL)

        exit(1);

    p2=malloc(sizeof(int)*5);

    if(p2==NULL)

        exit(1);

    memset(p1,0,sizeof(int)*10);

    memcpy(p2,p1,sizeof(int)*5);

    q=p2;

    for(i=0;i<5;i++)

        printf("%d",*(q++));

 

    free(p1);

    free(p2);

)

运行结果为5个0.

五、memmove

名称:

memmove

功能: 

拷贝(移动)内存空间

头文件:

#include <stdlib.h>

函数原形:

void *memmove(void *dest,void *src,unsigned int count);

参数: 

dest       目标内存区

src    原内存区

count      要复制的字节数

返回值: 

指向dest的指针

Memmove和函数memcpy函数功能一样,但只是拷贝内存空间,不处理空间重叠的问题.Memmove会处理空间重叠问题.当dest和src重叠时,仍能正确处理,但src内容发生改变.

六、memcmp

名称:

memcmp

功能: 

比较两个内存空间的字符

头文件:

#include <stdlib.h>

函数原形:

int memcmp(void *buf1,void *buf2,unsigned int count);

参数: 

buf1       内存区

buf2    内存区

count      要比较的字符数

返回值: 

见下面

Memcmp会比较内存区域buf1和buf2的前count个字节.Memcmp回根据ASCLL码表顺序依次比较.当buf1<buf2时,返回<0;当buf1=buf2时,返回0;当buf1>buf2时,返回>0.

main()

{

    int *p1=NULL;

    int *p2=NULL;

    int rt;

  

    p1=malloc(sizeof(int)*10);

    if(p1==NULL)

        exit(1);

    p2=malloc(sizeof(int)*10);

    if(p2==NULL)

        exit(1);

    memset(p1,'a',sizeof(int)*10);

    memset(p2,'b',sizeof(int)*10);

    rt=memcmp(p1,p2,sizeof(int)*10);

    if(rt>0)

        printf("p1>p2);

    if(rt<0)

        printf("p1<p2");

    if(rt==0)

        printf("p1=p2");

    

    free(p1);

    free(p2);

}

运行结果:p1<p2

 

 

 

原文地址:https://www.cnblogs.com/penghaibo/p/3602489.html