473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

realloc question

suppose we have:

ptr = malloc(LARGE_AMOUNT);

and the call to malloc is succesful (non-NULL return).

is there any possibility of a later call to realloc like this:

tmp = realloc(ptr,SMALLER_AMOUNT);

to return NULL?

IOW, is it possible for realloc to fail when used to *reduce* the size of an
allocated memory block?
Nov 13 '05 #1
6 7328
On Fri, 07 Nov 2003 15:21:40 +0000, Blah wrote:
suppose we have:

ptr = malloc(LARGE_AMOUNT);

and the call to malloc is succesful (non-NULL return).

is there any possibility of a later call to realloc like this:

tmp = realloc(ptr,SMALLER_AMOUNT);

to return NULL?

IOW, is it possible for realloc to fail when used to *reduce* the size of an
allocated memory block?


Yes. At least the standard allows it to do so.
I can't imagine it would actually happen on most implementations, but
you must always check the return value of realloc to be sure.

Nov 13 '05 #2
In <8M********************@news4.srv.hcvlny.cv.net> "Blah" <bl**@blah.com> writes:
suppose we have:

ptr = malloc(LARGE_AMOUNT);

and the call to malloc is succesful (non-NULL return).

is there any possibility of a later call to realloc like this:

tmp = realloc(ptr,SMALLER_AMOUNT);

to return NULL?

IOW, is it possible for realloc to fail when used to *reduce* the size of an
allocated memory block?


The comp.std.c party line is that *any* function that is allowed by the
standard to fail doesn't need an actual reason for failing. In
particular, even

tmp = realloc(ptr, LARGE_AMOUNT);

is allowed to fail, although this should be a no operation and there is
practically no difference if the function returns ptr or NULL.

But there are malloc implementations where there is a good reason for
failing in your scenario: the memory pool allocated for blocks of the
new size is full and the attempt to enlarge it by getting more memory
from the system has failed. By returning NULL, realloc lets you know
that your memory block still has its original size.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3

"Dan Pop" <Da*****@cern.ch> wrote in message
news:bo**********@sunnews.cern.ch...
In <8M********************@news4.srv.hcvlny.cv.net> "Blah" <bl**@blah.com> writes:
suppose we have:

ptr = malloc(LARGE_AMOUNT);

and the call to malloc is succesful (non-NULL return).

is there any possibility of a later call to realloc like this:

tmp = realloc(ptr,SMALLER_AMOUNT);

to return NULL?

IOW, is it possible for realloc to fail when used to *reduce* the size of anallocated memory block?
The comp.std.c party line is that *any* function that is allowed by the
standard to fail doesn't need an actual reason for failing.


That certainly doesn't sound encouraging.

Does that mean something along the lines of:

int fprintf()
{
static int num = 0;
if ( num++ % 2 )
return real_fprintf();
else
return fail_fprintf();
}

would be conforming? Just fail on every other call because it feels like
it?

In particular, even

tmp = realloc(ptr, LARGE_AMOUNT);

is allowed to fail, although this should be a no operation and there is
practically no difference if the function returns ptr or NULL.
Just to make sure I understand you, if I call realloc() requesting that the
size of the memory block *does not change* it can fail?
But there are malloc implementations where there is a good reason for
failing in your scenario: the memory pool allocated for blocks of the
new size is full and the attempt to enlarge it by getting more memory
from the system has failed. By returning NULL, realloc lets you know
that your memory block still has its original size.


Not that having a memory block reduction fail is something difficult to work
around, but out of curiosity are there any known implementations that do
occasionally fail in this scenario?
Nov 13 '05 #4

"Blah" <bl**@blah.com> wrote in message
news:mm*********************@news4.srv.hcvlny.cv.n et...

(snip unrelated to realloc())
Just to make sure I understand you, if I call realloc() requesting that the size of the memory block *does not change* it can fail?
But there are malloc implementations where there is a good reason for
failing in your scenario: the memory pool allocated for blocks of the
new size is full and the attempt to enlarge it by getting more memory
from the system has failed. By returning NULL, realloc lets you know
that your memory block still has its original size.
Not that having a memory block reduction fail is something difficult to

work around, but out of curiosity are there any known implementations that do
occasionally fail in this scenario?


Some versions of realloc() always allocate new memory, copy old to new, and
free the old. In that case, it could fail even for a smaller region. That
is, even when reducing the size of the allocated memory.

-- glen
Nov 13 '05 #5
"Blah" <bl**@blah.com> wrote:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:bo**********@sunnews.cern.ch...
The comp.std.c party line is that *any* function that is allowed by the
standard to fail doesn't need an actual reason for failing.


That certainly doesn't sound encouraging.

Does that mean something along the lines of:

int fprintf()
{
static int num = 0;
if ( num++ % 2 )
return real_fprintf();
else
return fail_fprintf();
}

would be conforming? Just fail on every other call because it feels like
it?


Yes. It wouldn't _sell_, of course, but it would be conforming. The
Standard does not (and can not) address issues of Quality of
Implementation; that is left to market forces, i.e., us.

Richard
Nov 13 '05 #6
On Mon, 10 Nov 2003 09:49:21 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
"Blah" <bl**@blah.com> wrote:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:bo**********@sunnews.cern.ch...
The comp.std.c party line is that *any* function that is allowed by the
standard to fail doesn't need an actual reason for failing.


That certainly doesn't sound encouraging.

Does that mean something along the lines of:

int fprintf()
{
static int num = 0;
if ( num++ % 2 )
return real_fprintf();
else
return fail_fprintf();
}

would be conforming? Just fail on every other call because it feels like
it?


Yes. It wouldn't _sell_, of course, but it would be conforming. The
Standard does not (and can not) address issues of Quality of
Implementation; that is left to market forces, i.e., us.

++nits: If it accepts and passes on the correct arguments/types, which
typically doesn't happen automatically for empty parens; and those
names, if actual functions, are either static (don't conflict with
user functions) or renamed to the implementation namespace;
and if signed-int overflow on that platform is well-defined or you use
unsigned instead. Although, if signed overflow wraps to negative and
%2 rounds down, as permitted in C89 and required in C99, it does not
fail as described = exactly every other call.

To extend the concept slightly, if( rand() %2U ), is nonconforming
because of 7.20.2.1p3: "The implementation shall behave as if no
library function calls the rand function." But
if ( __internalrand(&__separateseed) %2U ) is "OK".

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Henrik J | last post by:
Hello group... I have this little problem: I'm using a struct **foo. I have allocated x foo's using malloc: foo=(FOO**)malloc(Amount*sizeof(FOO*)); No problem....!
20
by: Jonas | last post by:
Hi, I'm 99 % sure that Standard C guarantees to do a memory move inside realloc() in case the new, returned memory block (address) is different than the original one. Can any C expert confirm...
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
5
by: James S. Singleton | last post by:
Thanks to everybody who provided an answer to my previous question on this. I am trying to grasp the nitty-gritty details of this, and I am having difficulties understanding some of the issues...
23
by: James Brown | last post by:
Hi all, I just wanted to make sure I understand realloc void *realloc( void *memblock, size_t size ); 1. when memblock is zero, realloc behaves like malloc 2. when memblock is non-zero, and...
27
by: Deephay | last post by:
Greetings all, I have a program that used the realloc() function to change the allocated size of a buffer, the program works with some arguments, but with some other arguments, it will show me...
31
by: banansol | last post by:
Hi, I just want to get this right. A call to realloc() will return NULL on error and the original memory is left untouched, both when requesting a larger or a smaller size that the original,...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
9
by: Guillaume Dargaud | last post by:
Hello all, I have a 'good practice' question. Lately I've been using a lot of functions such as this: void Func(int Size, double *Array) { static double *Transformed=NULL;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.