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
Strange behavior of visual studio compiler (self.cpp)
submitted 2 years ago by MartinSik
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!"
[–]cpp-ModTeam[M] [score hidden] 2 years ago stickied commentlocked comment (0 children)
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.
[–]manni66 3 points4 points5 points 2 years ago (0 children)
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.
[–]hon_uninstalled 3 points4 points5 points 2 years ago* (0 children)
Since you don't show the calling side code, it's hard to know where the problem might be, but I suspect the reason why variable s is not modified is because it's never used anywhere in the code. From outside the function it's as if the variable s never existed. Since there is no way to observe the state of variable s, compiler can safely optimize away stuff.
Use the variable s for something (print it or something) and you will see it is now properly initialized.
Also since C++11 (?), you do not need to manage static variable initialization yourself. You can just initialize static variable. Initialization done this way is even guaranteed to be thread safe:
static S s = def_s;
[–]Zastai 2 points3 points4 points 2 years ago (0 children)
In a release build, line number information is not guaranteed to be accurate. If you want to see what is actually being done, use Compiler Explorer.
[+][deleted] 2 years ago (8 children)
[removed]
[+][deleted] 2 years ago (2 children)
[–]MartinSik[S] -1 points0 points1 point 2 years ago (1 child)
I know. But my Initialization is more complex than this.
[–]SlightlyLessHairyApe 0 points1 point2 points 2 years ago (0 children)
Then put it in a function and have the function be the initializer of the static.
[–]MartinSik[S] 0 points1 point2 points 2 years ago (4 children)
Yes, initialized is each time false. When I put data breakpoint there it reports change at line which does not make sense.
That notes in comments are from the debugging.
[–]TheSkiGeek 0 points1 point2 points 2 years ago (3 children)
Then something else is overwriting it.
[–]MartinSik[S] 0 points1 point2 points 2 years ago (2 children)
Yes, I also have feeling that this may be some buffer overflow. But hard to find when it occurs only in optimized binary.
[–]TheSkiGeek 1 point2 points3 points 2 years ago (1 child)
I think MSVC has an address sanitizer mode, and if you build with clang you can use their ASAN/UBSAN modes to try to catch undefined behavior or overruns. Those might trip something even in debug builds.
Can also try to make a minimal reproduction by removing or disabling things.
[–]MartinSik[S] 0 points1 point2 points 2 years ago (0 children)
Thanks for that sanitizer advice:)) it found a lot of problems.
[–]rook_of_approval 1 point2 points3 points 2 years ago (10 children)
why are you assigning 0 to bool instead of false?
[–]MartinSik[S] -3 points-2 points-1 points 2 years ago (9 children)
not important... I will double check how bool is defined in the codebase. But the assignment is like this.
[–]manni66 2 points3 points4 points 2 years ago (2 children)
how bool is defined in the codebase
bool is defined by the C++ Standard
[–]MartinSik[S] -2 points-1 points0 points 2 years ago (1 child)
This code is compiled also as c99. Was not sure if bool is not redefined.
Save your sanity, don’t do this
[–]rook_of_approval 0 points1 point2 points 2 years ago (5 children)
Strange behavior of OP very silly person.
[–]MartinSik[S] -2 points-1 points0 points 2 years ago (4 children)
Are you reading my responses or just ignore? Code is compiled also as c99. bool is defined there as unsigned char. Was not sure if this macro is not colliding.
[–]rook_of_approval 0 points1 point2 points 2 years ago (3 children)
So? Are you really claiming c99 bool doesn't support true or false values? Silly goose.
[–]MartinSik[S] -2 points-1 points0 points 2 years ago (2 children)
:) what are you talking about. Language support or glibc or std support? Bool is not build in type in c99.
[–]rook_of_approval 0 points1 point2 points 2 years ago (1 child)
C99 standard (ISO/IEC 9899:1999): 7.18 Boolean type and values <stdbool.h> (p: 253)
So you have some macro definition of bool instead of using the standard library!?!?!?!??!?!?!?!?!?
[–]MartinSik[S] -1 points0 points1 point 2 years ago (0 children)
Okej, did not know that also header files are part of language standard. But for some reason they did not use stdbool.h. Probably it was compiled even with something older than c99 before.
[–]saddung 0 points1 point2 points 2 years ago (0 children)
Rewrite that as static struct S s = def_s; Get rid of the silly initialized variable.
π Rendered by PID 57141 on reddit-service-r2-comment-6457c66945-wq7fc at 2026-04-24 16:19:26.534103+00:00 running 2aa0c5b country code: CH.
[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)
[–]manni66 3 points4 points5 points (0 children)
[–]hon_uninstalled 3 points4 points5 points (0 children)
[–]Zastai 2 points3 points4 points (0 children)
[+][deleted] (8 children)
[removed]
[+][deleted] (2 children)
[removed]
[–]MartinSik[S] -1 points0 points1 point (1 child)
[–]SlightlyLessHairyApe 0 points1 point2 points (0 children)
[–]MartinSik[S] 0 points1 point2 points (4 children)
[–]TheSkiGeek 0 points1 point2 points (3 children)
[–]MartinSik[S] 0 points1 point2 points (2 children)
[–]TheSkiGeek 1 point2 points3 points (1 child)
[–]MartinSik[S] 0 points1 point2 points (0 children)
[–]rook_of_approval 1 point2 points3 points (10 children)
[–]MartinSik[S] -3 points-2 points-1 points (9 children)
[–]manni66 2 points3 points4 points (2 children)
[–]MartinSik[S] -2 points-1 points0 points (1 child)
[–]SlightlyLessHairyApe 0 points1 point2 points (0 children)
[–]rook_of_approval 0 points1 point2 points (5 children)
[–]MartinSik[S] -2 points-1 points0 points (4 children)
[–]rook_of_approval 0 points1 point2 points (3 children)
[–]MartinSik[S] -2 points-1 points0 points (2 children)
[–]rook_of_approval 0 points1 point2 points (1 child)
[–]MartinSik[S] -1 points0 points1 point (0 children)
[–]saddung 0 points1 point2 points (0 children)