From: "Dann Corbit" <dcorbit@solutionsiq.com>
Subject: Re: Why does my C Realloc() fail?
Date: 1998/04/09 [thread overview]
Message-ID: <6gjjk5$mb1$1@client2.news.psi.net> (raw)
In-Reply-To: 352d36a9.0@calwebnnrp
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 <stdio.h>
>#include <malloc.h>
>#include <stdlib.h>
>#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 <stdlib.h>
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
next prev parent reply other threads:[~1998-04-09 4:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-04-09 4:00 Why does my C Realloc() fail? Biju Srinivasan
1998-04-09 4:00 ` Dann Corbit [this message]
1998-04-10 4:00 ` Exil Q Trob
1998-04-10 4:00 ` Christopher Green
1998-04-11 4:00 ` Mark Brown
1998-04-22 4:00 ` Jeff Donsbach
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox