This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: malloc attributes and realloc
Jonathan Lennox <lennox@cs.columbia.edu> writes:
> Joseph S. Myers writes:
>> My current draft documentation change for attribute malloc (for this issue
>> and PR 3414) is
>
>> The @code{malloc} attribute is used to tell the compiler that a function
>> -may be treated as if it were the malloc function. The compiler assumes
>> -that calls to malloc result in pointers that cannot alias anything.
>> +may be treated as if any non-@code{NULL} pointer it returns cannot
>> +alias any other pointer valid when the function returns.
>> This will often improve optimization.
>> +Standard functions with this property include @code{malloc},
>> +@code{calloc} and @code{realloc}.
>
> This isn't directly relevant to the current thread, but another aspect of
> malloc-like functions came up recently: the memory they return is writable.
> I.e., if char *p was returned by an attribute(malloc) function, then
> if (*p != 'a')
> *p = 'a';
>
> can be safely optimized into
> *p = 'a';
... that is *not* an optimization; the cache behavior of the latter is
likely to be worse than the former.
zw