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 8938527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:38:47+00:00 2026-06-15T10:38:47+00:00

I know that global variables are created in declaration order for same compilation unit

  • 0

I know that global variables are created in declaration order for same compilation unit and creation order is not defined between multiple compilation units.

I’ve read some time ago that global variables are created before any code from the compilation unit where they are defined is called. Is this defined by the standard?

Example:

file1.cpp

int f1v1 = f1_1();
int f1v2 = f1_2();

void f1(){...}

int f1_1(){...}
int f1_2(){...}

file2.cpp

static int f2v1 = f2_1();
static int f2v2 = f2_2();

int f2_1(){...}
int f2_2(){...}

main.cpp

#include "file1.h"
#include "file2.h"

int main()
{
    f1();

    return 0;
}

Is in this case guaranteed by the standard that f1_1() is called before f1_2() and before f1()? Is guaranteed by the standard that f2_1() and f2_2() are called at all as no function defined in file2.cpp is called and f2v1 and f2v2 are not visible outside file2.cpp?

EDIT:

Is the behavior specified by the standard when file1.cpp is compiled in lib1 and file2.cpp is compiled in lib2?

  • 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-06-15T10:38:48+00:00Added an answer on June 15, 2026 at 10:38 am

    Based on [basic.start.init]:

    f1_1() is guaranteed to execute before f1_2() (para.2: “Variables with ordered initialization defined within a single translation unit shall be initialized in the order of their definitions in the translation unit.”).

    Both are guaranteed to execute before f1() (para.4: “If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use (3.2) of any function or variable defined in the same translation unit as the variable to be initialized.”).

    About f2_1() and f2_2(), [basic.stc.static]para.2 says “If a variable with static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, …”. This would imply they are guaranteed to be called if they contain side effects.

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

Sidebar

Related Questions

I know that, in Delphi, instance variables and global variables are initialized to zero
It's known that big local/global variables may cause to a stack overflow. I know
I have some global variables (classes) that as I understand have created before main()
I know that immutable objects always have the same state, the state in which
I know that in order to download a cookie associated with an account using
I know I should avoid using global variables in the first place due to
im trying to create global variables of wx.Panel, wx.Button, and wx.TextCtrl, so that i
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that if port 443 is open that means the remote host supports

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.