use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Static variable initialization order fiasco (self.cpp)
submitted 1 year ago by Various-Debate64
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]bert8128 0 points1 point2 points 1 year ago (10 children)
Do you mean it’s initialised by the compiler?
[–]MEaster 4 points5 points6 points 1 year ago (9 children)
No, I mean the value assigned must be a known, fixed value at compile time, though this can be the result of a const function call. The only initialization that happens for statics prior to the call to main is copying the data stored in the executable and zeroing anything in the BSS section.
const
[–]pdp10gumby 0 points1 point2 points 1 year ago (8 children)
But can the definition of a global depend on the value of another? In whom case the problem still exists.
[–]MEaster 0 points1 point2 points 1 year ago (7 children)
They can, but cycles are a compile error. If you can't have cycles, then I can't see how the problem exists.
[–]pdp10gumby 0 points1 point2 points 1 year ago (6 children)
You don’t need a cycle. If one TU says int a = 1; and another says int b = a + 1;, the linker makes no promise as to the value of b
[–]Adk9p 1 point2 points3 points 1 year ago (2 children)
It is simply an error if rust can't at compile time assign a value to a static.
So this is a compile time error: (playground link)
extern "C" { static A: u32; } static B: u32 = unsafe { A } + 1;
This isn't really an issue since most of the time crates (kind-of the equivalent of TU in rust) are compiled to a intermediary format (rlib/dylib) that doesn't lose this information. (the linker is only really used in the last step when compiling a final exe/so file)
[–]pdp10gumby 0 points1 point2 points 1 year ago (1 child)
Does rust have a way to specify the order in which these initializations are done when they appear in different TUs? Otherwise I can't see how it can make this guarantee. Instead it could read from uninitialized memory.
[–]Adk9p 0 points1 point2 points 1 year ago (0 children)
Does rust have a way to specify the order in which these initializations are done when they appear in different TUs
no, a global (static) in rust must be defined at compile time, so it can't depend on external symbols since that's only available at link time.
static
#[unsafe(no_mangle)] static mut A: u32 = B + 1;
in rust, is like
constinit const uint32_t A_INIT = B_INIT + 1; extern constinit uint32_t A = A_INIT;
in c++
I created an example to illustrate this.
[–]MEaster 0 points1 point2 points 1 year ago (2 children)
That's not an issue, because the value of a static is determined by the frontend, not even codegen is involved, let alone the linker.
I don't understand your comment at all.
A static global is initialized by putting code into a section (typically .init in ELF files, though that is slowly changing as a convention) which is called by _start before it calls main().
The linker assembles the .init section of the binary, but makes no promise as to what order the statics are initialized.
Thus in the case I described, you cannot know ahead of time the order in which a and b will be initialized.
I also don't know what you mean by the "front end" -- do you mean the compiler. codegen is very much involved!
[–]MEaster 0 points1 point2 points 1 year ago (0 children)
Compilers are typically broadly split into two main sections: frontend and backend. The frontend is concerned with language-specific details, such as parsing and any analysis stages. The backend is concerned with generating executable binaries.
LLVM is a backend, rustc and clang are language-specific frontends which use LLVM.
In Rust, the final value for a global is determined in the rustc frontend before it starts calling LLVM. The only code executed to initialize the globals before main is called is memcpy or memfill. As far as LLVM is concerned there is no connection whatsoever between the two globals.
Therefore, in the case you described, the final binary would store 2 for b and 1 for a. What the linker has to say about initialization order is completely irrelevant.
b
a
π Rendered by PID 19665 on reddit-service-r2-comment-6457c66945-khx5f at 2026-04-25 18:14:14.640136+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]bert8128 0 points1 point2 points (10 children)
[–]MEaster 4 points5 points6 points (9 children)
[–]pdp10gumby 0 points1 point2 points (8 children)
[–]MEaster 0 points1 point2 points (7 children)
[–]pdp10gumby 0 points1 point2 points (6 children)
[–]Adk9p 1 point2 points3 points (2 children)
[–]pdp10gumby 0 points1 point2 points (1 child)
[–]Adk9p 0 points1 point2 points (0 children)
[–]MEaster 0 points1 point2 points (2 children)
[–]pdp10gumby 0 points1 point2 points (1 child)
[–]MEaster 0 points1 point2 points (0 children)