From: sinbad on
i want to write a constructor and destructor function in c for each of
the function.
I should register a routines which will be called before calling the
function and destructor before actual return of the original function.
Does gcc provides anything like this.

thanks
sinbad
From: fred.l.kleinschmidt on
On Apr 8, 11:26 pm, sinbad <sinbad.sin...(a)gmail.com> wrote:
> i want to write a constructor and destructor function in c for each of
> the function.
> I should register a routines which will be called before calling the
> function and destructor before actual return of the original function.
> Does gcc provides anything like this.
>
> thanks
> sinbad

C does not have constructors or destructors.
--
Fred Kleinschmidt
From: William Pursell on
On Apr 9, 7:26 am, sinbad <sinbad.sin...(a)gmail.com> wrote:
> i want to write a constructor and destructor function in c for each of
> the function.
> I should register a routines which will be called before calling the
> function and destructor before actual return of the original function.
> Does gcc provides anything like this.

gcc allows you to put a cleanup attribute on one of the
automatic variables which would give you the destructor.
http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Variable-Attributes.html#Variable%20Attributes

To simulate a constructor, you can overload the function
by giving your constructor the same name as the
function and then use dlsym( RTLD_NEXT,...) to
call the function itself.

You're probably better off just modifying
the function to start by calling your constructor/initializer
and end by calling the destructor.
From: Charles Coldwell on
sinbad <sinbad.sinbad(a)gmail.com> writes:

> i want to write a constructor and destructor function in c for each of
> the function.
> I should register a routines which will be called before calling the
> function and destructor before actual return of the original function.
> Does gcc provides anything like this.

I don't completely understand the question, but maybe this does what you
want?

#include <stdio.h>

static __attribute__((constructor)) void ctor(void)
{
puts("Hello, world from ctor");
}

int main(int argc, char *argv[])
{
puts("Hello, world from main");
return 0;
}

$ gcc foo.c && ./a.out
Hello, world from ctor
Hello, world from main

Chip

--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
GPG Key ID: 852E052F
GPG Key Fingerprint: 77E5 2B51 4907 F08A 7E92 DE80 AFA9 9A8F 852E 052F
 | 
Pages: 1
Prev: A way to time-limit a program in C
Next: Daemons