public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* __attribute__ ((malloc)) semantics?
@ 2003-05-09 23:48 Martin Dickopp
  2003-05-11  2:08 ` Michael Meissner
  0 siblings, 1 reply; 4+ messages in thread
From: Martin Dickopp @ 2003-05-09 23:48 UTC (permalink / raw)
  To: gcc-help

Hi,

I'm a bit confused what the effect of __attribute__ ((malloc)) is.
The GCC manual says:

    The `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 a pointers that cannot
    alias anything.  This will often improve optimization.

What does is mean to treat a function as if it were the malloc function,
other than assuming that the returned pointer does not alias anything?

E.g., would it be safe to use __attribute__ ((malloc)) on the following
function?

    int *foo (void) { static int i; return &i; }

Thanks,
Martin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: __attribute__ ((malloc)) semantics?
  2003-05-09 23:48 __attribute__ ((malloc)) semantics? Martin Dickopp
@ 2003-05-11  2:08 ` Michael Meissner
  2003-05-11 12:58   ` Martin Dickopp
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Meissner @ 2003-05-11  2:08 UTC (permalink / raw)
  To: Martin Dickopp; +Cc: gcc-help

On Sat, May 10, 2003 at 01:47:59AM +0200, Martin Dickopp wrote:
> Hi,
> 
> I'm a bit confused what the effect of __attribute__ ((malloc)) is.
> The GCC manual says:
> 
>     The `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 a pointers that cannot
>     alias anything.  This will often improve optimization.
> 
> What does is mean to treat a function as if it were the malloc function,
> other than assuming that the returned pointer does not alias anything?
> 
> E.g., would it be safe to use __attribute__ ((malloc)) on the following
> function?
> 
>     int *foo (void) { static int i; return &i; }

No, because repeated calls to foo will return the same pointer, which would
create aliases.

-- 
Michael Meissner
email: meissner@the-meissners.org
http://www.the-meissners.org

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: __attribute__ ((malloc)) semantics?
  2003-05-11  2:08 ` Michael Meissner
@ 2003-05-11 12:58   ` Martin Dickopp
  2003-05-15  1:02     ` Michael Meissner
  0 siblings, 1 reply; 4+ messages in thread
From: Martin Dickopp @ 2003-05-11 12:58 UTC (permalink / raw)
  To: gcc-help

On Sat, May 10, 2003 at 10:08:39PM -0400, Michael Meissner wrote:
> On Sat, May 10, 2003 at 01:47:59AM +0200, Martin Dickopp wrote:
> > E.g., would it be safe to use __attribute__ ((malloc)) on the following
> > function?
> > 
> >     int *foo (void) { static int i; return &i; }
> 
> No, because repeated calls to foo will return the same pointer, which
> would create aliases.

Let me rephrase my question: Could it be safe to use __attribute__
((malloc)) on the above function in some circumstances?

What if any pointer previously returned by foo is considered to become
invalid every time foo is called? Even repreated calls to malloc (with
intervening calls to free) can return the same address.

Martin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: __attribute__ ((malloc)) semantics?
  2003-05-11 12:58   ` Martin Dickopp
@ 2003-05-15  1:02     ` Michael Meissner
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Meissner @ 2003-05-15  1:02 UTC (permalink / raw)
  To: Martin Dickopp; +Cc: gcc-help

On Sun, May 11, 2003 at 02:58:40PM +0200, Martin Dickopp wrote:
> On Sat, May 10, 2003 at 10:08:39PM -0400, Michael Meissner wrote:
> > On Sat, May 10, 2003 at 01:47:59AM +0200, Martin Dickopp wrote:
> > > E.g., would it be safe to use __attribute__ ((malloc)) on the following
> > > function?
> > > 
> > >     int *foo (void) { static int i; return &i; }
> > 
> > No, because repeated calls to foo will return the same pointer, which
> > would create aliases.
> 
> Let me rephrase my question: Could it be safe to use __attribute__
> ((malloc)) on the above function in some circumstances?
> 
> What if any pointer previously returned by foo is considered to become
> invalid every time foo is called? Even repreated calls to malloc (with
> intervening calls to free) can return the same address.

I suspect if you can guarantee that all references to the previous pointer are
now dead (as far as the compiler is concerned), it will work.  The problem is
as optimization technology improves things that used to work in one revision
may not work in a future revision.

-- 
Michael Meissner
email: meissner@the-meissners.org
http://www.the-meissners.org

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-05-15  1:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-09 23:48 __attribute__ ((malloc)) semantics? Martin Dickopp
2003-05-11  2:08 ` Michael Meissner
2003-05-11 12:58   ` Martin Dickopp
2003-05-15  1:02     ` Michael Meissner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).