Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:12:33+00:00 2026-05-27T09:12:33+00:00

Example: struct Foo { Foo() { printf(foo\n); } }; static Foo foo; __attribute__((constructor)) static

  • 0

Example:

struct Foo { Foo() { printf("foo\n"); } };
static Foo foo;

__attribute__((constructor)) static void _bar() { printf("bar\n"); }

Is it deterministic wether foo or bar is printed first?

(I hope and would expect that constructors of static objects are always executed first but not sure and GCCs doc about the constructor attribute doesn’t say anything about it.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:12:34+00:00Added an answer on May 27, 2026 at 9:12 am

    foo will be printed first, as the objects are initialized in the order of their declarations. Run and see:

    • Ideone online demo

    By the way, __attribute__((constructor)) is not Standard C++. It is GCC’s extension. So the behavior of your program depends on how GCC has defined it. In short, it is implementation-defined, according to it foo is printed first.

    The doc says,

    The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () has completed or exit () has been called. Functions with these attributes are useful for initializing data that will be used implicitly during the execution of the program.

    You may provide an optional integer priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors. So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects (see C++ Attributes).

    I think the text in bold implies, the objects are initialized in the order of their declarations, as I said before, which is pretty much confirmed by online demo also.

    I guess you would also like to read this:

    • 7.7 C++-Specific Variable, Function, and Type Attributes

    If you want to control/alter the initialization order, you can use init_priority attribute, providing priority. Taken from the page:

    Some_Class  A  __attribute__ ((init_priority (2000)));
    Some_Class  B  __attribute__ ((init_priority (543)));
    

    Here, B is initialized before A.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a struct struct Foo { void bar () { do_baz(this);
Consider this smallest example (I could think of): struct Bar; struct Foo { Bar*
If I have a struct like so: struct example { uint16_t bar:1; uint16_t foo:1;
For example: struct Foo { int bar; int (*baz)(int); }; int testFunc(int x) {
Probably best explained with an example: struct A { virtual void foo() = 0;
Taking the following snippet as an example: struct Foo { typedef int type; };
typedef struct foo { bool my_bool; int my_int; } foo; In the example above
Is the following example a valid complete translation unit in C? struct foo; struct
I have some function templates, for example template <typename T> void foo(T); template <typename
Consider the following example: struct Scanner { template <typename T> T get(); }; template

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.