This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


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

Re: Patch: malloc function attribute


> 
>   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.


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