#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[])
{
#if 0
// malloc 返回值是 void * , 所以每次使用都得强制类型转换...略坑...
// 怎么在已经静态分配的 数组上增加 大小呢>>>???
// int * p = (int *)malloc(4 * sizeof(int));
// memset(p, '\0', 16); // malloc 不会初始化, 还得调用 memset 初始化 <string.h>
// calloc(块数, 每个块的大小(占几个字节))
// calloc 自带初始化功能
int * p = calloc(4, sizeof(int));
// 使用一个函数给malloc申请的空间进行初始化
for (int i = 0; i < 4; i++) {
*(p + i) = arc4random_uniform(10);
}
for (int i = 0; i < 4; i++) {
printf("%d\n", *(p + i));
}
#endif
int * pp = (int *)calloc(4, sizeof(int));
for (int i = 0; i < 4; i++) {
*(pp + i) = arc4random_uniform(10);
printf("%d\t", *(pp + i));
}
printf("\n=================\n");
// 多出来的空间 没有初始化...
//9 7 9 4 2074935312 32767 -1906251453 32767 2074960072 32767 -1906251447 32767 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
pp = realloc(pp, 40 * sizeof(int));
for (int i = 0; i < 40; i++) {
printf("%d\t", *(pp + i));
}
return 0;
}