The constructor and destructor function attributes

The constructor and destructor function attributes provide the ability to write a function that initializes data or releases storage that is used implicitly during program execution. A function to which the constructor function attribute has been applied is called automatically before execution enters main. Similarly, a function to which the destructor attribute has been applied is called automatically after calling exit or upon completion of main.

When the constructor or destructor function is called automatically, the return value of the function is ignored, and any parameters of the function are undefined.

Read syntax diagramSkip visual syntax diagramconstructor and destructor function attribute syntax
 
>>-__attribute__--((--+-constructor-----+--))------------------><
                      +-destructor------+
                      +-__constructor__-+
                      '-__destructor__--'
 

A function declaration containing a constructor or destructor function attribute must match all of its other declarations.

Related information