From: hipermar AT mail2 DOT esoterica DOT pt (Hiper M.A.R.) Newsgroups: comp.os.msdos.djgpp Subject: Malloc and realloc problems Date: Mon, 18 Sep 2000 10:29:56 GMT Organization: Esoterica, Portugal Lines: 115 Message-ID: <969276437.311068@osiris.esoterica.pt> NNTP-Posting-Host: osiris-ip.esoterica.pt X-Trace: news.interpacket.net 969272975 88134 209.198.242.35 (18 Sep 2000 10:29:35 GMT) X-Complaints-To: usenet AT news DOT interpacket DOT net NNTP-Posting-Date: 18 Sep 2000 10:29:35 GMT X-Newsreader: Forte Free Agent 1.0.82 Cache-Post-Path: osiris.esoterica.pt!unknown AT por411 DOT esoterica DOT pt X-Cache: nntpcache 2.3.3 (see http://www.nntpcache.org/) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Incoerent results are given deppending on how memory is malloc or realloc: a)Using a straight foward malloc b)Trying to malloc in 1MB step c)Trying to realloc in 1 to 10 MB steps. Using the programs listen at the bottom, i came the the following results, using a computer with 128 MB ram and win98 1st edition: Step Success Fail a) 124 125 b) 95 96 c) 1 35 36 2 42 44 3 45 48 4 48 52 5 45 50 6 54 60 7 63 70 8 48 56 9 54 63 10 60 70 What on earth this means...??? Can someone help...? Thanks. //-------------------------------------------------------------------- // a) Straight malloc #include #include int main(void) { void *ptr; int step; step=124; //change this value; recompile to find max. available ptr = malloc(1048576*step); if (!ptr) { printf("Fail to allocate %d MB of memory!\n", step); return 0; } else { printf("Successfuly allocated %d MB\n", step); free(ptr); } return 0; } //--------------------------------------------------------------------- // b) Trying to malloc in 1MB step #include #include int main(void) { void *ptr; int step, i; step=1; //in 1MB step for(i=1;i<257;i++) { ptr = malloc(1048576*step*i); if (!ptr) { printf("Fail to malloc %d MB of memory!\n", step*i); return 0; } else { printf("Successfuly malloc %d MB\n", step*i); free(ptr); } } return 0; } //--------------------------------------------------------------------- // c)Trying to realloc in 1 to 10 MB step. #include #include int main(void) { void *ptr; int step, i; ptr = malloc(1048576*1); if (!ptr) { printf("Fail to malloc 1 MB of memory!\n"); return 0; } else { printf("Successfuly malloc 1 MB\n"); } step=1; //change the setp from 1 to 10, and recompile for(i=1;i<257;i++) { ptr = realloc(ptr, 1048576*step*i); if (!ptr) { printf("Fail to realloc %d MB of memory!\n", step*i); return 0; } else { printf("Successfuly realloc %d MB\n", step*i); } } return 0; }