From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,c617820a3952d228 X-Google-Attributes: gidfee84,public X-Google-Thread: 1014db,c617820a3952d228 X-Google-Attributes: gid1014db,public From: "Dann Corbit" Subject: Re: Why does my C Realloc() fail? Date: 1998/04/09 Message-ID: <6gjjk5$mb1$1@client2.news.psi.net>#1/1 X-Deja-AN: 342556840 Content-Transfer-Encoding: 7bit References: <352d36a9.0@calwebnnrp> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 Organization: PSINet Newsgroups: comp.lang.c,comp.unix.osf.osf1 Biju Srinivasan wrote in message <352d36a9.0@calwebnnrp>... >Platform = DEC Alpha (OSF1 V4.0) >Why does my C realloc() fail? >A single malloc works for as big as 134 MBytes. >Realloc fails for a size of 101 MBytes following an alloc of 100 MBytes. >Doesn't realloc claim the space assigned to the original pointer and add >extra space to it? > >I get the following error when i run the following piece of code >"Error in realloc:: Not enough space" > >Begin: Include Code >========== >#include >#include >#include >#define STR_LEN 134000000 A single malloc with this size succeeds. >#define STR_LEN1 100000000 >#define STR_LEN2 101000000 >main() >{ > char *tStr; > if((tStr = (char *)malloc(STR_LEN1)) == NULL) > perror("Error in malloc:"); > if((tStr = (char *)realloc(tStr, STR_LEN2)) == NULL) > perror("Error in realloc:"); >} >========== >End: Include Code Runs fine for me on a pee-cee. I should think your process would need rights to ~230 megs ram to complete successfully. Man page says this: "Function void * realloc (void *ptr, size_t newsize) The realloc function changes the size of the block whose address is ptr to be newsize. Since the space after the end of the block may be in use, realloc may find it necessary to copy the block to a new address where more free space is available. The value of realloc is the new address of the block. If the block needs to be moved, realloc copies the old contents. ..." The draft C9X spec says the following: 7.14.3.4 The realloc function Synopsis [#1] #include void *realloc(void *ptr, size_t size); Description [#2] The realloc function changes the size of the object pointed to by ptr to the size specified by size. The contents of the object shall be unchanged up to the lesser of the new and old sizes. If the new size is larger, the value of the newly allocated portion of the object is indeterminate. If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size. Otherwise, if ptr does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to the free or realloc function, the behavior is undefined. If the space cannot be allocated, the object pointed to by ptr is unchanged. If size is zero and ptr is not a null pointer, the object it points to is freed. Returns [#3] The realloc function returns either a null pointer or a pointer to the possibly moved allocated space. If a pointer is returned which does not compare equal to ptr, then the object has moved and ptr is a pointer that refers to freed space. -- Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9 Try "C Programming: A Modern Approach" ISBN 0-393-96945-2 Want Software? Algorithms? Pubs? http://www.infoseek.com