#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int *p = NULL;
p = (int *)malloc(5*sizeof(int));
memset(p,0,5*sizeof(int));
int i = 0;
for(i = 0;i<5;++i)
{
printf("%d\n",*(p+i));
}
free(p);
p=NULL;
int *c = NULL;
c = (int *)calloc(5,sizeof(int));
for(i = 0;i<5;++i)
{
printf("%d\n",*(c+i));
}
free(c);
c = NULL;
int *r = NULL;
for(i = 0;i<5;++i)
{
r = (int *)realloc(r,(i+1)*sizeof(int));
}
memset(r,0,5*sizeof(int));
for(i = 0;i<5;++i)
{
printf("%d\n",*(r+i));
}
free(r);
r = NULL;
return 0;
}
malloc/calloc/realloc
最新推荐文章于 2022-11-05 19:56:53 发布