#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <time.h>
int main() {
// 内存管理
char name[50];
char *desc;
strcpy(name, "jiangzhihao");
//calloc(200, sizeof(char));
//在内存中动态地分配 num 个长度为 size 的连续空间,并将每一个字节都初始化为 0。
//所以它的结果是分配了 num*size 个字节长度的内存空间,并且每个字节的值都是0。
//malloc(30 * sizeof(char))
//在堆区分配一块指定大小的内存空间,用来存放数据。这块内存空间在函数执行完成后不会被初始化,它们的值是未知的。
// 动态分配内存
desc = (char *)malloc(30 * sizeof(char));
if (desc == NULL) {
fprintf(stderr, "Error - unable to allocate required memory\n");
} else {
strcpy(desc, "chanpinxue.cn");
}
// 假设要存储更大的描述信息
desc = realloc(desc, 50 * sizeof(char));
if (desc == NULL) {
fprintf(stderr, "Error - unable to allocate required memory\n");
} else {
strcat(desc, "\twelcome to chanpinxue.cn");
}
printf("Name = %s\n", name);
printf("Desc: %s\n", desc);
// 使用 free() 函数释放内存
free(desc);
}