This is the mail archive of the
mailing list for the GCC project.
Re: malloc attributes and realloc
- From: Jeff Sturm <jsturm at one-point dot com>
- To: "Joseph S. Myers" <jsm at polyomino dot org dot uk>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 1 Jan 2004 10:31:00 -0500 (EST)
- Subject: Re: malloc attributes and realloc
On Thu, 1 Jan 2004, Joseph S. Myers wrote:
> Can __attribute__((__malloc__)) safely be used on realloc-type functions?
My non-expert understanding is "no"...
> /* Reallocate memory without fail. This works like xmalloc. Note,
> realloc type functions are not suitable for attribute malloc since
> they may return the same address across multiple calls. */
This comment is misleading. The pointer address returned by a malloc-type
function isn't significant to alias analysis. What *is* significant is
that malloc returns memory initialized with zeros, so that it cannot
alias anything. (Moreover, malloc itself can return the same address
twice if a call to free intervenes between calls to malloc.)
Since the return value of realloc() aliases whatever its first argument
aliases, a literal reading of the documentation of the malloc attribute
indicates that it is inappropriate for the declaration of realloc.