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: implement __attribute__((cleanup(function)))

On May  5, 2003, Richard Henderson <> wrote:

> +@item cleanup (@var{cleanup_function})
> +@cindex @code{cleanup} attribute
> +The @code{cleanup} attribute runs a function when the variable goes
> +out of scope.  This attribute can only be applied to auto function
> +scope variables; it may not be applied to parameters or variables
> +with static storage duration.  The function must take one parameter,
> +a pointer to a type compatible with the variable.  The return value
> +of the function (if any) is ignored.
> +
> +If @option{-fexceptions} is enabled, then @var{cleanup_function}
> +will be run during the stack unwinding that happens during the
> +processing of the exception.  Note that the @code{cleanup} attribute
> +does not allow the exception to be caught, only to perform an action.
> +It is undefined what happens if @var{cleanup_function} does not
> +return normally.

How about:

  int i = 0, j = 0;

        auto void f(int **);
        int *pi __attribute__((cleanup(f))) = &i;
        void f(int **ppi) { **ppi = 1; pi = &j; }
        pi; }))
      printf ("true\n"):
      printf ("false\n");

when f() is executed, is pi still in scope?  But then, why is f()
executed?  What is the expected output of this program?

This extension is obviously not as simple as one might think.  I still
think coming up with a new extension is a mistake.  I'd much rather go
with try/finally, a well-known extension that is likely to be
standardized, than coming up with a new extension with odd
interactions with other extensions that we already have.

Alexandre Oliva   Enjoy Guarana', see
Red Hat GCC Developer                 aoliva@{,}
CS PhD student at IC-Unicamp        oliva@{,}
Free Software Evangelist                Professional serial bug killer

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