C语言中内存操作函数

一、malloc/calloc

名称:

Malloc/calloc

功能: 

动态内存分配函数

头文件:

#include <stdlib.h>

函数原形:

void *malloc(size_t size);

void *calloc(size_t num,size_t size);

参数

size    分配内存块的大小

num  分配内存块的个数

返回值: 

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

malloccalloc都可以分配内存区,但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/n");

        exit(1);

    }

    *p=3;

    printf("%d/n",*p);

    free(p);

}

 

 

二、free

名称

free

功能 

动态内存释放函数

头文件

#include <stdlib.h>

函数原形

void free(void *ptr);

参数

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

返回值 

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

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

三、memset

名称:

memset

功能: 

初始化所指定的内存空间

头文件:

#include <stdlib.h>

函数原形:

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

参数

buffer      分配的内存

     初始化内容

count      初始化的字节数   

返回值: 

返回指向buffer的指针

memsetbuffer所指内存区域的前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所指内存区.如果countsrc字节数大,strcpy会拷贝'/0'后结束.要注意destsrc不要重叠.

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会处理空间重叠问题.当destsrc重叠时,仍能正确处理,但src内容发生改变.

六、memcmp

名称:

memcmp

功能: 

比较两个内存空间的字符

头文件:

#include <stdlib.h>

函数原形:

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

参数

buf1       内存区

buf2    内存区

count      要比较的字符数

返回值: 

见下面

Memcmp会比较内存区域buf1buf2的前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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值