* Re: Why does my C Realloc() fail?
1998-04-09 4:00 Why does my C Realloc() fail? Biju Srinivasan
@ 1998-04-09 4:00 ` Dann Corbit
1998-04-10 4:00 ` Christopher Green
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Dann Corbit @ 1998-04-09 4:00 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: Why does my C Realloc() fail?
1998-04-09 4:00 Why does my C Realloc() fail? Biju Srinivasan
1998-04-09 4:00 ` Dann Corbit
@ 1998-04-10 4:00 ` Christopher Green
1998-04-10 4:00 ` Exil Q Trob
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Christopher Green @ 1998-04-10 4:00 UTC (permalink / raw)
In article <352d36a9.0@calwebnnrp>,
Biju Srinivasan <biju@web1.calweb.com> wrote:
>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?
[snip]
Typically, realloc() allocates a new block of the requested size, copies
the old block to it, then frees the old block and returns the new block.
The situation where there is a large free block immediately following
the old block is rare; some realloc()'s may be able to take advantage
of it, but you can't count on it.
In this case, the problem may be that you have exceeded the per-process
heap size limit or the paging (swap) file size when realloc() tried to
allocate the new block, and that is why realloc() failed. You can raise
the per-process limit with ulimit up to some system-specified maximum;
you have to get your sysadmin to raise the maximum to go beyond that.
Digital UNIX (which still calls itself OSF1 when you invoke uname) has
two different swap behaviors. The default behavior is conservative:
anytime you allocate memory, there must be free space in the swap file
for it. The alternative is optimistic behavior: the system will allo-
cate memory freely, but will fail if it has to be swapped and the swap
file is already full. See the man page for swapon for details.
--
Chris Green Email cgreen@atc.com
Advanced Technology Center Phone (714) 583-9119
22982 Mill Creek Drive ext. 220
Laguna Hills, CA 92653 Fax (714) 583-9213
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: Why does my C Realloc() fail?
1998-04-09 4:00 Why does my C Realloc() fail? Biju Srinivasan
1998-04-09 4:00 ` Dann Corbit
1998-04-10 4:00 ` Christopher Green
@ 1998-04-10 4:00 ` Exil Q Trob
1998-04-11 4:00 ` Mark Brown
1998-04-22 4:00 ` Jeff Donsbach
4 siblings, 0 replies; 6+ messages in thread
From: Exil Q Trob @ 1998-04-10 4:00 UTC (permalink / raw)
In article <352d36a9.0@calwebnnrp>, biju@web1.calweb.com says...
>
>Doesn't realloc claim the space assigned to the original pointer and add
>extra space to it?
Only if it *can*. It is perfectly legal for an implementation to never even
try to do this.
>A single malloc works for as big as 134 MBytes.
>Realloc fails for a size of 101 MBytes following an alloc of 100 MBytes.
I worked on a system, a few years back, where this sequence would have
failed. Let's assume that 134MB can work because that's all the free space
that the OS has to give the application.
So the app asks for 100MB, and the OS gives it a block that big.
Then it reallocs for 101MB. That won't fit in the current block -- so ask
the OS for a block it will fit in. But the OS has only 34MB available -- can't
make 101 from that.
Note that there's no mechanism for joining a block previously obtained from
the OS to a new block. It seems to me that any such mechanism would be
terribly brittle, subject to easy breakage.
David G
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Why does my C Realloc() fail?
1998-04-09 4:00 Why does my C Realloc() fail? Biju Srinivasan
` (2 preceding siblings ...)
1998-04-10 4:00 ` Exil Q Trob
@ 1998-04-11 4:00 ` Mark Brown
1998-04-22 4:00 ` Jeff Donsbach
4 siblings, 0 replies; 6+ messages in thread
From: Mark Brown @ 1998-04-11 4:00 UTC (permalink / raw)
biju@web1.calweb.com (Biju Srinivasan) writes:
> 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?
It needn't. realloc() may simply work by getting the amount of memory
you requested and copying the data from the pointer you passed
(there's not even any requirement that it free the existing memory,
although there are very valid quality of implementation issues with an
implementation that doesn't).
To understand why the implementation would do this, consider the case
where thee is a block of memory allocated immediately after the one
being realloc()ed. How could one both increase the size of the first
block and keep pointers to the second block valid?
You are almost certainly going to need to try to process the data in
smaller sections - 100MB of memory is an awful lot of memory to be
using.
[followups to comp.lang.c - I've no idea about how OSF/1 handles this]
--
Mark Brown mailto:broonie@tardis.ed.ac.uk (Trying to avoid grumpiness)
http://www.tardis.ed.ac.uk/~broonie/
EUFS http://www.ed.ac.uk/~filmsoc/
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: Why does my C Realloc() fail?
1998-04-09 4:00 Why does my C Realloc() fail? Biju Srinivasan
` (3 preceding siblings ...)
1998-04-11 4:00 ` Mark Brown
@ 1998-04-22 4:00 ` Jeff Donsbach
4 siblings, 0 replies; 6+ messages in thread
From: Jeff Donsbach @ 1998-04-22 4:00 UTC (permalink / raw)
You are probably hitting the default data segment limit for a process.
In your C Shell before running your program, say "unlimit datasize".
This will allow your data segment to grow up to 1GB. For more than that,
you need to change some kernel parameters and reboot.
-Jeff
--
=========================
Jeff Donsbach Digital Equipment Corporation, Software Partners Engineering
200 Forest St., MRO1-3/F26, Marlborough, MA 01752-3011
Email: Jeff.Donsbach@digital.com "10 bucks is 10 bucks" - Geddy Lee
^ permalink raw reply [flat|nested] 6+ messages in thread