This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: malloc attributes and realloc
- From: Jeff Sturm <jsturm at one-point dot com>
- To: Gabriel Dos Reis <gdr at integrable-solutions dot net>
- Cc: Daniel Berlin <dberlin at dberlin dot org>, <gcc at gcc dot gnu dot org>, "Joseph S. Myers" <jsm at polyomino dot org dot uk>
- Date: Thu, 1 Jan 2004 17:06:35 -0500 (EST)
- Subject: Re: malloc attributes and realloc
On 1 Jan 2004, Gabriel Dos Reis 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.
> |
> | This isn't true.
> | It's quite significant, because it means it can't be the same pointer
> | as before.
>
> Well, technically, it could be as hinted by the standard:
>
> [#4] The realloc function returns a pointer to the new
> object (which may have the same value as a pointer to the
> old object), or a null pointer if the new object could not
> be allocated.
So, out of curiosity, what happens in an example like:
#include <stdlib.h>
int f(void) {
int *p, *q;
p = malloc(sizeof(int));
*p = 0;
q = realloc(p, sizeof(int));
if (p == q) {
*p = 1;
return *q;
}
return 0;
}
Does the alias machinery determine that p,q alias after all? Or is this
illegal for some reason?
Jeff