C专栏D-内存管理

来源:互联网 发布:淘宝天猫图标 编辑:程序博客网 时间:2024/05/29 17:57

/*************************************************

function:c language Technology
author  :chinayaosir   QQ:44633197 
blog    :http://blog.csdn.net/chinayaosir
note    :禁止其它网站转载此文章

**************************************************/

1.内存分配释放函数
1.1 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不会进行初始化.
codesample:

#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=100;
    printf("%d/n",*p);
    free(p);
}

1.2 free
名称: free
功能: 动态内存释放函数
头文件: #include <stdlib.h>
函数原形: void free(void *ptr);
参数:  ptr   
使用malloc或calloc等内存分配函数所返回的内存指针
返回值: 无
 
free 可以释放由malloc或calloc等内存分配函数分配的内存.
当程序很大时,期间可能要多次动态分配内存,如果不及时释放的话,程序将要占用很大内存.
要注意,如果ptr所指内存已被释放或是未知的内存地址,则可能有无法预期的情况发生.若参数为NULL,则free不会有任何作用.
codesample:    free(p);

2.memset
名称: memset
功能: 初始化所指定的内存空间
头文件: #include <stdlib.h>
函数原形: void *memset(void *buffer,int c,int count);
参数:  
buffer    分配的内存
c     初始化内容
count     初始化的字节数   
返回值:  返回指向buffer的指针
memset把buffer所指内存区域的前count个字节设置成某个字符的ASCLL值.
一般用于给数组,字符串等类型赋值.
codesample:
#include <stdio.h>
#include <stdlib.h>
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.
0000000000

3.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会拷贝'/0'后结束.
要注意dest和src不要重叠.
memcpy只是拷贝内存空间,不处理空间重叠的问题.
codesample:

#include <stdio.h>
#include <stdlib.h>
main()
{
int *p1=NULL;
int *p2=NULL;
int *q,*i;
p1=(int *)malloc(sizeof(int)*10);
p2=(int *)malloc(sizeof(int)*5);
if(p1==NULL)       exit(1);
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个00.

4.memmove

名称: memmove/memcpy
功能: 拷贝(移动)内存空间
头文件: #include <stdlib.h>
函数原形: void *memmove(void *dest,void *src,unsigned int count);
参数:
dest       目标内存区
src    原内存区
count      要复制的字节数
返回值: 指向dest的指针

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


5.memmove
名称: 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.
#include <stdio.h>
#include <stdlib.h>
main()
{
    int *p1=NULL;
    int *p2=NULL;
    int rt;

    p1=(int *)malloc(sizeof(int)*10);
    if(p1==NULL)        exit(1);
    p2=(int *)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
 
原创粉丝点击