This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Patch: malloc function attribute
- To: law at cygnus dot com
- Subject: Re: Patch: malloc function attribute
- From: Richard Earnshaw <rearnsha at arm dot com>
- Date: Tue, 30 Nov 1999 17:19:22 +0000
- Cc: Mark Mitchell <mark at codesourcery dot com>, ghazi at caip dot rutgers dot edu, green at cygnus dot com, gcc-patches at gcc dot gnu dot org
- Cc: richard dot earnshaw at arm dot com
- Organization: ARM Ltd.
- Reply-To: richard dot earnshaw at arm dot com
>
> In message <19991129074845D.mitchell@codesourcery.com>you write:
> > I believe that if you know something about your realloc
> > implementation, then you can know that realloc will return the same
> > pointer. So, I can imagine something like:
> >
> > int *ip = (int *) malloc (8);
> > int *ip2 = (int* ) realloc (ip, 16);
> >
> > and that the programmer knows that `ip' and `ip2' *are* aliases. I've
> > just looked at the C standard and I don't see any reason to believe
> > that using `ip' in this code yields undefined behavior,
> Egad. I'll accept this. I don't have anything to counter it. With this
> in mind we need to not consider realloc or xrealloc as being malloc-like.
> Bummer.
>
> > Maybe I'm off base, but I thought that the malloc attribute said the
> > new pointer doesn't alias anything.
> You're correct.
> jeff
>
I can't see why any reasonable programmer would expect that to work...
However, a much more reasonable expectation is that
int *ip = (int *)malloc(16);
int *ip2 = (int *)realloc(ip, 8);
foo(*ip);
would do the right thing in that it would release the tail of the
allocated memory area, but not move things about. This would be
sufficient to break the no-aliasing assumptions.
R.