Skip to content

Instantly share code, notes, and snippets.

@denisdemaisbr
Created November 8, 2024 15:55
Show Gist options
  • Save denisdemaisbr/84d9052a322881ed34d1b7a10615dc7c to your computer and use it in GitHub Desktop.
Save denisdemaisbr/84d9052a322881ed34d1b7a10615dc7c to your computer and use it in GitHub Desktop.
a c example of constructor/destructor
/*
https://www.geeksforgeeks.org/functions-that-are-executed-before-and-after-main-in-c/
*/
#include<stdio.h>
/* Apply the constructor attribute to myStartupFun() so that it
is executed before main() */
void myStartupFun (void) __attribute__ ((constructor));
/* Apply the destructor attribute to myCleanupFun() so that it
is executed after main() */
void myCleanupFun (void) __attribute__ ((destructor));
/* implementation of myStartupFun */
void myStartupFun (void)
{
printf ("startup code before main()\n");
}
/* implementation of myCleanupFun */
void myCleanupFun (void)
{
printf ("cleanup code after main()\n");
}
int main (void)
{
printf ("hello\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment