验证malloc分配内存能力
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#define SIZE 5024000 //19GB
#define SIZE 102400
typedef struct bean {
int16_t index;
char name[1024*1024] ;
} bean;
int main() {
bean *b;
char *p=NULL;
p="aadf";
char a[10][10];
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
a[i][j]='A';
}
}
printf("bean size %ld\n", sizeof(bean));
printf("bean size %ld\n", SIZE*sizeof(bean));
b=malloc(SIZE*sizeof(bean));
printf("b %p\n",b);
printf("b+0 %p\n",b+0);
printf("b size %ld\n", sizeof(*b));
bean* t=b;
for (int k = 0; k < SIZE; ++k) {
t->index=k;
sprintf(t->name,"BB %d",k);
//printf("%d\n",k);
t++;
}
//
// for (int l = 0; l < SIZE; ++l) {
//
// printf("name:%s\n", b[l].name);
// printf("index:%d\n", b[l].index);
//
// }
printf("%ld\n",sizeof(*b));
free(b);
return 0;
}
malloc申请内存后不会立刻分配,实际在for循环赋值时才真正分配内存
内存分配xcode截图