메모리 할당 왜 이럴까?

음... 첫번째 시도와 두번째 시도의
차이는 for 중간에 있는
이것을 풀고 안풀고의 차이인데,,
//memset (ptr, 0x00, alloc_mem);
이 현상의 원인이 무엇일까요?
# 테스트 환경
Linux version 2.4.13 (gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release))
#include
#include
#include
#include
#include
int main() {
int i = 0;
long alloc_mem = 0;
double tot_mem = 0.0;
char *ptr;
for (;;i++) {
alloc_mem = 1024 * 1024 * i;
ptr = malloc(alloc_mem);
if (ptr == NULL) exit(0);
//memset (ptr, 0x00, alloc_mem);
tot_mem += (double)(alloc_mem);
printf("i[%d] alloc_mem[%d]kb tot_mem[%0.f]mb ptr[0x%08x]\n",
i, alloc_mem / 1024, tot_mem / (1024 * 1024), ptr);
}
return 1;
}
첫번째 시도
[choissi@hyuna choissi]$ ./mem
i[0] alloc_mem[0]kb tot_mem[0]mb ptr[0x080496a8]
i[1] alloc_mem[1024]kb tot_mem[1]mb ptr[0x4010b008]
i[2] alloc_mem[2048]kb tot_mem[3]mb ptr[0x4020c008]
i[3] alloc_mem[3072]kb tot_mem[6]mb ptr[0x4040d008]
i[4] alloc_mem[4096]kb tot_mem[10]mb ptr[0x4070e008]
i[5] alloc_mem[5120]kb tot_mem[15]mb ptr[0x40b0f008]
i[6] alloc_mem[6144]kb tot_mem[21]mb ptr[0x41010008]
i[7] alloc_mem[7168]kb tot_mem[28]mb ptr[0x41611008]
i[8] alloc_mem[8192]kb tot_mem[36]mb ptr[0x41d12008]
i[9] alloc_mem[9216]kb tot_mem[45]mb ptr[0x42513008]
i[10] alloc_mem[10240]kb tot_mem[55]mb ptr[0x42e14008]
i[11] alloc_mem[11264]kb tot_mem[66]mb ptr[0x43815008]
i[12] alloc_mem[12288]kb tot_mem[78]mb ptr[0x44316008]
i[13] alloc_mem[13312]kb tot_mem[91]mb ptr[0x44f17008]
i[14] alloc_mem[14336]kb tot_mem[105]mb ptr[0x45c18008]
i[15] alloc_mem[15360]kb tot_mem[120]mb ptr[0x46a19008]
i[16] alloc_mem[16384]kb tot_mem[136]mb ptr[0x4791a008]
i[17] alloc_mem[17408]kb tot_mem[153]mb ptr[0x4891b008]
i[18] alloc_mem[18432]kb tot_mem[171]mb ptr[0x49a1c008]
i[19] alloc_mem[19456]kb tot_mem[190]mb ptr[0x4ac1d008]
i[20] alloc_mem[20480]kb tot_mem[210]mb ptr[0x4bf1e008]
i[21] alloc_mem[21504]kb tot_mem[231]mb ptr[0x4d31f008]
i[22] alloc_mem[22528]kb tot_mem[253]mb ptr[0x4e820008]
i[23] alloc_mem[23552]kb tot_mem[276]mb ptr[0x4fe21008]
i[24] alloc_mem[24576]kb tot_mem[300]mb ptr[0x51522008]
i[25] alloc_mem[25600]kb tot_mem[325]mb ptr[0x52d23008]
i[26] alloc_mem[26624]kb tot_mem[351]mb ptr[0x54624008]
i[27] alloc_mem[27648]kb tot_mem[378]mb ptr[0x56025008]
i[28] alloc_mem[28672]kb tot_mem[406]mb ptr[0x57b26008]
i[29] alloc_mem[29696]kb tot_mem[435]mb ptr[0x59727008]
i[30] alloc_mem[30720]kb tot_mem[465]mb ptr[0x5b428008]
i[31] alloc_mem[31744]kb tot_mem[496]mb ptr[0x5d229008]
i[32] alloc_mem[32768]kb tot_mem[528]mb ptr[0x5f12a008]
i[33] alloc_mem[33792]kb tot_mem[561]mb ptr[0x6112b008]
i[34] alloc_mem[34816]kb tot_mem[595]mb ptr[0x6322c008]
i[35] alloc_mem[35840]kb tot_mem[630]mb ptr[0x6542d008]
i[36] alloc_mem[36864]kb tot_mem[666]mb ptr[0x6772e008]
i[37] alloc_mem[37888]kb tot_mem[703]mb ptr[0x69b2f008]
i[38] alloc_mem[38912]kb tot_mem[741]mb ptr[0x6c030008]
i[39] alloc_mem[39936]kb tot_mem[780]mb ptr[0x6e631008]
i[40] alloc_mem[40960]kb tot_mem[820]mb ptr[0x70d32008]
i[41] alloc_mem[41984]kb tot_mem[861]mb ptr[0x73533008]
i[42] alloc_mem[43008]kb tot_mem[903]mb ptr[0x75e34008]
i[43] alloc_mem[44032]kb tot_mem[946]mb ptr[0x78835008]
i[44] alloc_mem[45056]kb tot_mem[990]mb ptr[0x7b336008]
i[45] alloc_mem[46080]kb tot_mem[1035]mb ptr[0x7df37008]
i[46] alloc_mem[47104]kb tot_mem[1081]mb ptr[0x80c38008]
i[47] alloc_mem[48128]kb tot_mem[1128]mb ptr[0x83a39008]
i[48] alloc_mem[49152]kb tot_mem[1176]mb ptr[0x8693a008]
i[49] alloc_mem[50176]kb tot_mem[1225]mb ptr[0x8993b008]
i[50] alloc_mem[51200]kb tot_mem[1275]mb ptr[0x8ca3c008]
i[51] alloc_mem[52224]kb tot_mem[1326]mb ptr[0x8fc3d008]
i[52] alloc_mem[53248]kb tot_mem[1378]mb ptr[0x92f3e008]
i[53] alloc_mem[54272]kb tot_mem[1431]mb ptr[0x9633f008]
i[54] alloc_mem[55296]kb tot_mem[1485]mb ptr[0x99840008]
i[55] alloc_mem[56320]kb tot_mem[1540]mb ptr[0x9ce41008]
i[56] alloc_mem[57344]kb tot_mem[1596]mb ptr[0xa0542008]
i[57] alloc_mem[58368]kb tot_mem[1653]mb ptr[0xa3d43008]
i[58] alloc_mem[59392]kb tot_mem[1711]mb ptr[0xa7644008]
i[59] alloc_mem[60416]kb tot_mem[1770]mb ptr[0xab045008]
i[60] alloc_mem[61440]kb tot_mem[1830]mb ptr[0xaeb46008]
i[61] alloc_mem[62464]kb tot_mem[1891]mb ptr[0xb2747008]
i[62] alloc_mem[63488]kb tot_mem[1953]mb ptr[0xb6448008]
i[63] alloc_mem[64512]kb tot_mem[2016]mb ptr[0xba249008]
i[64] alloc_mem[65536]kb tot_mem[2080]mb ptr[0x080496b8]
i[65] alloc_mem[66560]kb tot_mem[2145]mb ptr[0x0c0496c0]
i[66] alloc_mem[67584]kb tot_mem[2211]mb ptr[0x101496c8]
i[67] alloc_mem[68608]kb tot_mem[2278]mb ptr[0x143496d0]
i[68] alloc_mem[69632]kb tot_mem[2346]mb ptr[0x186496d8]
i[69] alloc_mem[70656]kb tot_mem[2415]mb ptr[0x1ca496e0]
i[70] alloc_mem[71680]kb tot_mem[2485]mb ptr[0x20f496e8]
i[71] alloc_mem[72704]kb tot_mem[2556]mb ptr[0x255496f0]
i[72] alloc_mem[73728]kb tot_mem[2628]mb ptr[0x29c496f8]
i[73] alloc_mem[74752]kb tot_mem[2701]mb ptr[0x2e449700]
i[74] alloc_mem[75776]kb tot_mem[2775]mb ptr[0x32d49708]
i[75] alloc_mem[76800]kb tot_mem[2850]mb ptr[0x37749710]
두번째 시도
[choissi@hyuna choissi]$ ./mem
i[0] alloc_mem[0]kb tot_mem[0]mb ptr[0x08049710]
i[1] alloc_mem[1024]kb tot_mem[1]mb ptr[0x4010b008]
i[2] alloc_mem[2048]kb tot_mem[3]mb ptr[0x4020c008]
i[3] alloc_mem[3072]kb tot_mem[6]mb ptr[0x4040d008]
i[4] alloc_mem[4096]kb tot_mem[10]mb ptr[0x4070e008]
i[5] alloc_mem[5120]kb tot_mem[15]mb ptr[0x40b0f008]
i[6] alloc_mem[6144]kb tot_mem[21]mb ptr[0x41010008]
i[7] alloc_mem[7168]kb tot_mem[28]mb ptr[0x41611008]
i[8] alloc_mem[8192]kb tot_mem[36]mb ptr[0x41d12008]
i[9] alloc_mem[9216]kb tot_mem[45]mb ptr[0x42513008]
i[10] alloc_mem[10240]kb tot_mem[55]mb ptr[0x42e14008]
i[11] alloc_mem[11264]kb tot_mem[66]mb ptr[0x43815008]
i[12] alloc_mem[12288]kb tot_mem[78]mb ptr[0x44316008]
i[13] alloc_mem[13312]kb tot_mem[91]mb ptr[0x44f17008]
i[14] alloc_mem[14336]kb tot_mem[105]mb ptr[0x45c18008]
i[15] alloc_mem[15360]kb tot_mem[120]mb ptr[0x46a19008]
i[16] alloc_mem[16384]kb tot_mem[136]mb ptr[0x4791a008]
i[17] alloc_mem[17408]kb tot_mem[153]mb ptr[0x4891b008]
i[18] alloc_mem[18432]kb tot_mem[171]mb ptr[0x49a1c008]
i[19] alloc_mem[19456]kb tot_mem[190]mb ptr[0x4ac1d008]
i[20] alloc_mem[20480]kb tot_mem[210]mb ptr[0x4bf1e008]
i[21] alloc_mem[21504]kb tot_mem[231]mb ptr[0x4d31f008]
i[22] alloc_mem[22528]kb tot_mem[253]mb ptr[0x4e820008]
i[23] alloc_mem[23552]kb tot_mem[276]mb ptr[0x4fe21008]
i[24] alloc_mem[24576]kb tot_mem[300]mb ptr[0x51522008]
i[25] alloc_mem[25600]kb tot_mem[325]mb ptr[0x52d23008]
i[26] alloc_mem[26624]kb tot_mem[351]mb ptr[0x54624008]
i[27] alloc_mem[27648]kb tot_mem[378]mb ptr[0x56025008]
i[28] alloc_mem[28672]kb tot_mem[406]mb ptr[0x57b26008]
i[29] alloc_mem[29696]kb tot_mem[435]mb ptr[0x59727008]
i[30] alloc_mem[30720]kb tot_mem[465]mb ptr[0x5b428008]
i[31] alloc_mem[31744]kb tot_mem[496]mb ptr[0x5d229008]
i[32] alloc_mem[32768]kb tot_mem[528]mb ptr[0x5f12a008]
i[33] alloc_mem[33792]kb tot_mem[561]mb ptr[0x6112b008]
i[34] alloc_mem[34816]kb tot_mem[595]mb ptr[0x6322c008]
i[35] alloc_mem[35840]kb tot_mem[630]mb ptr[0x6542d008]
i[36] alloc_mem[36864]kb tot_mem[666]mb ptr[0x6772e008]
i[37] alloc_mem[37888]kb tot_mem[703]mb ptr[0x69b2f008]
i[38] alloc_mem[38912]kb tot_mem[741]mb ptr[0x6c030008]
i[39] alloc_mem[39936]kb tot_mem[780]mb ptr[0x6e631008]
i[40] alloc_mem[40960]kb tot_mem[820]mb ptr[0x70d32008]
i[41] alloc_mem[41984]kb tot_mem[861]mb ptr[0x73533008]
i[42] alloc_mem[43008]kb tot_mem[903]mb ptr[0x75e34008]
댓글 달기