#include<stdlib.h> #include<stdio.h> #include<string.h> #define SIZE 500 intmain(int argc, char **argv) { int max = -1; int mb = 0; char *buffer; int i; unsignedint *p = malloc(1024 * 1024 * SIZE);
printf("malloc buffer: %p\n", p);
for (i = 0; i < 1024 * 1024 * (SIZE/sizeof(int)); i++) { p[i] = 123; if ((i & 0xFFFFF) == 0) { printf("%dMB written\n", i >> 18); usleep(100000); } } pause(); return0; }
编译: gcc test.c -o a.out
运行测试
执行程序./a.out 输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
malloc buffer: 0x7f32c6e85010 0MB written 4MB written 8MB written 12MB written 16MB written 20MB written 24MB written 28MB written 32MB written 36MB written 40MB written 44MB written 48MB written Killed
malloc buffer: 0x7fa2aba58010 0MB written 4MB written 8MB written 12MB written 16MB written 20MB written 24MB written 28MB written 32MB written 36MB written 40MB written 44MB written 48MB written 52MB written 56MB written 60MB written 64MB written 68MB written 72MB written 76MB written 80MB written 84MB written 88MB written 92MB written 96MB written 100MB written 104MB written 108MB written 112MB written 116MB written ^C