This is the mail archive of the 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]
Other format: [Raw text]

Re: malloc attributes and realloc

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';

GCC doesn't currently do this optimization for malloc-returned pointers (it
does for pointers to objects on the stack), but it could.  Would it be worth
adding to the documentation?

Jonathan Lennox
lennox at cs dot columbia dot edu

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