Go Back   Coding Forum > Coding World > C

Reply
 
LinkBack Thread Tools Display Modes
Old 08-28-2010, 08:41 PM   #1 (permalink)
Vincenzo Mercuri
Guest
 
Posts: n/a
Default freeing memory after realloc with size==0

Hi all,

lately I wanted to use the 'realloc' function at each iteration
in a loop, the i-th iteration being something like:

...

if ( !(temp[i] = realloc(ptr[i], n--)) ){
  Reply With Quote
Old 08-28-2010, 08:41 PM   #2 (permalink)
Vincenzo Mercuri
Guest
 
Posts: n/a
Default Re: freeing memory after realloc with size==0


> the realloc(temp[MAX], 0) in the previous iteration, mean


previous 'loop' instead of 'iteration'.

--
Vincenzo Mercuri
  Reply With Quote
Old 09-01-2010, 01:17 AM   #3 (permalink)
Marcin Grzegorczyk
Guest
 
Posts: n/a
Default Re: freeing memory after realloc with size==0

Vincenzo Mercuri wrote:
>[...]
> 7.20.3.4 The realloc function
>[...]
> /Description/
> p2 _The realloc function deallocates the old object pointed to by_
> _ptr and returns a pointer to a new object that has the size_
> _specified by size_. The contents of the new object shall be the
> same as that of the old object prior to deallocation, up to the
> lesser of the new and old sizes. Any bytes in the new object
> beyond the size of the old object have indeterminate values.
>
> p3 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 memory for the new object cannot be allocated, the old object
> is not deallocated and its value is unchanged.
>
>[...]
> C99 says nothing specific to the case size == 0; what do the
> words 'deallocates/deallocated' mean in the lines I have
> underlined?


Exactly what it says on the tin: the old object is deallocated.
realloc(), however, may not return a pointer to a deallocated object,
although it may return a null pointer (for instance, if zero-size
allocations are not supported).

> Does the case size == 0 falls in p2 (I'll get a
> pointer to a new object with size 0? and then may I free it?)
> or in p3 (assumed ptr != NULL, is the behavior undefined?) ??


It falls under the last sentence of 7.20.3p1: either you get a pointer
to a new object (that you're not allowed to access), or you get a null
pointer -- it's implementation-defined which, but either way, the
returned value may be legally passed to realloc() or free().
--
Marcin Grzegorczyk
  Reply With Quote
Old 09-04-2010, 12:17 AM   #4 (permalink)
Vincenzo Mercuri
Guest
 
Posts: n/a
Default Re: freeing memory after realloc with size==0

Marcin Grzegorczyk wrote:

[...]
> Exactly what it says on the tin: the old object is deallocated.
> realloc(), however, may not return a pointer to a deallocated object,
> although it may return a null pointer (for instance, if zero-size
> allocations are not supported).
>
>> Does the case size == 0 falls in p2 (I'll get a
>> pointer to a new object with size 0? and then may I free it?)
>> or in p3 (assumed ptr != NULL, is the behavior undefined?) ??

>
> It falls under the last sentence of 7.20.3p1: either you get a pointer
> to a new object (that you're not allowed to access), or you get a null
> pointer -- it's implementation-defined which, but either way, the
> returned value may be legally passed to realloc() or free().



Thank you for your response.


--
Vincenzo Mercuri
  Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT. The time now is 02:35 PM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Copyright ©2010, CodingForum.Org