public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
* Adding a realloc() usage note to the malloc(3) manual page
@ 2021-08-31  6:00 Michael Kerrisk (man-pages)
  2021-08-31  7:07 ` Florian Weimer
  0 siblings, 1 reply; 7+ messages in thread
From: Michael Kerrisk (man-pages) @ 2021-08-31  6:00 UTC (permalink / raw)
  To: libc-alpha@sourceware.org, linux-man
  Cc: Alejandro Colomar (man-pages), Florian Weimer, Paul Eggert

Hello all,

I plan to add a note on the correct usage of realloc() to the
malloc(3) manual page. I would be happy to receive comments and
improvements to the text below.

Thanks,

Michael


   Correct usage of realloc() (and reallocarray())
       Since, on the one hand, realloc() (and  reallocarray())  may  move
       the block of memory, and on the other, it may return NULL on fail‐
       ure and leave the memory contents and location unchanged,  correct
       usage is something like the following:

           void *ptr, *nptr;
           ptr = malloc(origsize);
           ...
           /* In the following, we presume 'newsize' is not 0.
              (If 'newsize' is zero, realloc() may return NULL,
              and that is not an error.) */

           nptr = realloc(ptr, newsize);
           if (nptr == NULL) {
               /* Handle error; the block pointed to by 'ptr' is
                  still usable. */
           } else {
               /* realloc() succeeded; update 'ptr' to point to
                  the (possibly moved) block. */
               ptr = nptr;
           }

       Similar remarks apply for reallocarray().
-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-09-02  7:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-08-31  6:00 Adding a realloc() usage note to the malloc(3) manual page Michael Kerrisk (man-pages)
2021-08-31  7:07 ` Florian Weimer
2021-08-31  9:29   ` Paul Eggert
2021-09-02  0:21     ` Michael Kerrisk (man-pages)
2021-09-02  1:23       ` Paul Eggert
2021-09-02  5:22         ` Michael Kerrisk (man-pages)
2021-09-02  7:46           ` Paul Eggert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox