|
#include<stdio.h>
void main()
{void *p,*q;
int m=30,n=0;
p=(void *)malloc(n);
for(n=1;n<=m;n++)
{q=p;
p=(void *)malloc(n);
printf("malloc(%d)=%p,malloc(%d)=%p,the increase is %d\n",n-1,q,n,p,p-q);
}
}
输出结果:
malloc(0)=0x86f6008,malloc(1)=0x86f6018,the increase is 16
malloc(1)=0x86f6018,malloc(2)=0x86f6028,the increase is 16
malloc(2)=0x86f6028,malloc(3)=0x86f6038,the increase is 16
malloc(3)=0x86f6038,malloc(4)=0x86f6048,the increase is 16
malloc(4)=0x86f6048,malloc(5)=0x86f6058,the increase is 16
malloc(5)=0x86f6058,malloc(6)=0x86f6068,the increase is 16
malloc(6)=0x86f6068,malloc(7)=0x86f6078,the increase is 16
malloc(7)=0x86f6078,malloc(8)=0x86f6088,the increase is 16
malloc(8)=0x86f6088,malloc(9)=0x86f6098,the increase is 16
malloc(9)=0x86f6098,malloc(10)=0x86f60a8,the increase is 16
malloc(10)=0x86f60a8,malloc(11)=0x86f60b8,the increase is 16
malloc(11)=0x86f60b8,malloc(12)=0x86f60c8,the increase is 16
malloc(12)=0x86f60c8,malloc(13)=0x86f60d8,the increase is 16
malloc(13)=0x86f60d8,malloc(14)=0x86f60f0,the increase is 24
malloc(14)=0x86f60f0,malloc(15)=0x86f6108,the increase is 24
malloc(15)=0x86f6108,malloc(16)=0x86f6120,the increase is 24
malloc(16)=0x86f6120,malloc(17)=0x86f6138,the increase is 24
malloc(17)=0x86f6138,malloc(18)=0x86f6150,the increase is 24
malloc(18)=0x86f6150,malloc(19)=0x86f6168,the increase is 24
malloc(19)=0x86f6168,malloc(20)=0x86f6180,the increase is 24
malloc(20)=0x86f6180,malloc(21)=0x86f6198,the increase is 24
malloc(21)=0x86f6198,malloc(22)=0x86f61b8,the increase is 32
malloc(22)=0x86f61b8,malloc(23)=0x86f61d8,the increase is 32
malloc(23)=0x86f61d8,malloc(24)=0x86f61f8,the increase is 32
malloc(24)=0x86f61f8,malloc(25)=0x86f6218,the increase is 32
malloc(25)=0x86f6218,malloc(26)=0x86f6238,the increase is 32
malloc(26)=0x86f6238,malloc(27)=0x86f6258,the increase is 32
malloc(27)=0x86f6258,malloc(28)=0x86f6278,the increase is 32
malloc(28)=0x86f6278,malloc(29)=0x86f6298,the increase is 32
malloc(29)=0x86f6298,malloc(30)=0x86f62c0,the increase is 40
我的问题是:这个大小是怎么确定的呢?为什么不直接是n的大小,还有在malloc(13)的时候为什么增量是24呢,16不是也够吗?
怎么理解呢? |
|