C标准库之内存操作函数——realloc
功能:realloc函数为一块已经分配了的连续空间重新分配内存函数原型:void * realloc(void * memblock, size_t size);
参数:
参数说明
memblock已分配内存的首地址
size重新分配的字节数
返回值:重新分配的内存块首地址
要求:
函数需要的头文件
realloc<malloc.h>或<stdlib.h>
举例:#include <stdio.h>
#include <malloc.h>
int main(void)
{
int * buf;
buf = (int *)malloc(20 * sizeof(int));//获得一块整型数组空间, 20个元素
printf("已分配内存大小:%d\n", _msize(buf));
buf = (int *)realloc(buf, 100 * sizeof(int));//重新分配为100个元素
printf("重新分配内存大小:%d\n", _msize(buf));
free(buf);//释放数组空间
return 0;
}
运行结果:
页:
[1]