Re: Documentation error or what am I missing?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Aug 10, 2012 at 6:28 PM, Oliver Schneider <oliver@xxxxxxxxxx> wrote:
>
> over at <http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html> one
> can read the following:
>
> ----
> malloc
> The malloc attribute is used to tell the compiler that a function may be
> treated as if any non-NULL pointer it returns cannot alias any other
> pointer valid when the function returns and that the memory has
> undefined content. This will often improve optimization. Standard
> functions with this property include malloc and calloc. realloc-like
> functions do not have this property as the memory pointed to does not
> have undefined content.
> ---
>
> I'm confused about the mentioning of calloc() in this place. Isn't it
> one of the semantic differences between malloc() and calloc() that the
> buffer whose address is returned by calloc() is zero-initialized?

The most important aspect of this attribute is that the pointers
returned by malloc and/or calloc can not alias any other pointer.

A secondary aspect is that the memory has undefined contents.  While
it is true that calloc returns zeroed memory, what the compiler really
cares about here is the type of the memory.  That is, does the memory
hold ints, floats, pointers, whatever.  And in that sense the memory
allocated by calloc is indeed undefined.  It does not hold any
particular type.

One could imagine the compiler optimizing in some way based on the
fact that calloc returns zero-initialized memory, but I think it is
unlikely that that would ever make a different in a real program, so I
doubt it is worth implementing.

Ian


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux