结构体的内存再开辟时会对齐
malloc函数,该函数就是为一个数组开辟空间
calloc函数,该函数就是为一个数组开辟空间,并将数据初始化为零
realloc函数,改变malloc和calloc开辟的空间的大小
这些所开辟的空间都被存放在堆区
int main()
{
char *str;
str = (char *) malloc(15);
strcpy(str, "runoob");
printf("String = %s, Address = %u\n", str, str);
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %u\n", str, str);
free(str);
return(0);
}
该代码的malloc为str开辟了15个字节,而如果用calloc那就并把str初始化为0
而后的realloc将malloc开辟的15个增加为25个
这些所开辟的空间都可以被free()释放