How to tell if realloc() succeeds? 
Author Message
 How to tell if realloc() succeeds?

How can I tell if resizing a memory block using realloc() succeeds?
For my platform, the following description is given for realloc():

   void *realloc( void *memblock, size_t size );

   realloc returns a void pointer to the reallocated (and possibly
   moved) memory block. The return value is NULL if the size is
   zero and the buffer argument is not NULL, or if there is not
   enough available memory to expand the block to the given size.
   In the first case, the original block is freed. In the second,
   the original block is unchanged. The return value points to a
   storage space that is guaranteed to be suitably aligned for
   storage of any type of object. To get a pointer to a type other
   than void, use a type cast on the return value.

So, if I want to make a memory block larger, but there isn't enough
memory, it will do nothing and just return my old pointer. How should
I know that it failed?

Thanks,
Leon.



Fri, 04 Feb 2005 02:08:17 GMT  
 How to tell if realloc() succeeds?

Quote:

> How can I tell if resizing a memory block using realloc() succeeds?

If it returns a non-null pointer, or if the size argument is
zero, it succeeded, and the old pointer is invalidated.

[...]

Quote:
> So, if I want to make a memory block larger, but there isn't enough
> memory, it will do nothing and just return my old pointer.

No, it will return a null pointer.

Quote:
> How should I know that it failed?

When it returns a null pointer (and you passed a nonzero size
argument), it failed.


Fri, 04 Feb 2005 02:18:27 GMT  
 How to tell if realloc() succeeds?

Quote:

> How can I tell if resizing a memory block using realloc() succeeds?
> [snipped]
> So, if I want to make a memory block larger, but there isn't enough
> memory, it will do nothing and just return my old pointer. How should
> I know that it failed?

If you reread the sentence you posted yourself:

Quote:
>    ... The return value is NULL if the size is
>    zero and the buffer argument is not NULL, or if there is not
>    enough available memory to expand the block to the given size.

it should be clear: realloc returns NULL if there isn't enough memory,
*not* the old pointer. That's why you should store the old pointer
before calling realloc or you may end up with a memory leak. And
realloc() may return the old pointer if there was still enough room
following the memory you already had allocated.

                                      Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring



Fri, 04 Feb 2005 02:24:35 GMT  
 How to tell if realloc() succeeds?

Quote:

> How can I tell if resizing a memory block using realloc() succeeds?
> For my platform, the following description is given for realloc():

>    void *realloc( void *memblock, size_t size );

>    realloc returns a void pointer to the reallocated (and possibly
>    moved) memory block. The return value is NULL if the size is
>    zero and the buffer argument is not NULL, or if there is not
>    enough available memory to expand the block to the given size.
>    In the first case, the original block is freed. In the second,
>    the original block is unchanged. The return value points to a
>    storage space that is guaranteed to be suitably aligned for
>    storage of any type of object. To get a pointer to a type other
>    than void, use a type cast on the return value.

> So, if I want to make a memory block larger, but there isn't enough
> memory, it will do nothing and just return my old pointer.

That's not what it says. It says the return value is NULL if there isn't
enough memory.

Here is the canonical way to use realloc. In this example, oldptr is of
type T* :

T *spareptr = realloc(oldptr, newnumelems * sizeof *oldptr);

if(spareptr != NULL)
{
  oldptr = spareptr;

Quote:
}

else
{
  /* the realloc failed, but at least you still have oldptr pointing to
the original memory block */

Quote:
}
> How should
> I know that it failed?

By checking for NULL.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Fri, 04 Feb 2005 04:35:52 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How can I tell if realloc() has worked?

2. malloc()/realloc() vs realloc()

3. Realloc failure when downsizing (was Re: Probably a simple realloc() question)

4. VC++ fails, BCB succeeds on console C program

5. (Q) URGENT - CDaoDatabase doesn't succeed, fail or except in Release mode

6. I succeeded to BindToObject..What then?

7. GetProcInstance failed but succeeded!?

8. GetProcADDRESSfailed but succeeded!?

9. fopen succeeds, fgets stack dumps

10. CFtpFileFind.FindFile succeeds, GetFile fails

11. LoadCursor succeeds where FindResource fails

12. (Q) CDaoDatabase Open not failing or succeeding!

 

 
Powered by phpBB® Forum Software