|
|
#3 (permalink) |
|
Guest
Posts: n/a
|
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 |
|
|
|
#4 (permalink) |
|
Guest
Posts: n/a
|
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 |
|