This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFC][PATCH] Add reallocarray function.

On 05/18/2014 10:32 PM, RÃdiger Sonderfeld wrote:
+/* Re-allocate the previously allocated block in PTR, making the new
+   block large enough for NMEMB elements of SIZE bytes each.  */
+/* __attribute_malloc__ is not used, because if realloc returns
+   the same pointer that was passed to it, aliasing needs to be allowed
+   between objects pointed by the old and new pointers.  */
+extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
+     __THROW __attribute_warn_unused_result__;

I'm not sure if this is still on the table, but experience shows that the realloc interface is error-prone for another reason: The straight way to write an a reallocation,

  ptr = realloc(ptr, new_size);

leads to a memory leak on error. It would be less error-prone to have reallocarray to update the pointer directly on success, e.g.:

  if (reallocarray(&ptr, new_count, sizeof(T)) < 0) {
    // handle error

However, this cannot be implemented as a C function, only as a macro.
Florian Weimer / Red Hat Product Security

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]