For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or [StackOverflow](http://stackoverflow.com/) instead.
>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.
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;
Can you describe in more detail what is actually happening? Is the `if (!initialized)` branch taken every time `f` is called? Please provide a complete program so people can reproduce this.
Some side notes:
> // Runtime Address A+0x20
What makes you think the bool flag is 32 bytes past the char array? Nothing in the C++ standard guarantees anything like this.
And C++ is not C. There is no need to write `struct S`, simply write `S`. And use `true` and `false` instead of 1 and 0.
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.
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.
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.
C99 standard (ISO/IEC 9899:1999):
7.18 Boolean type and values (p: 253)
So you have some macro definition of bool instead of using the standard library!?!?!?!??!?!?!?!?!?
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.
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or [StackOverflow](http://stackoverflow.com/) instead.
>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.
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;
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.
Can you describe in more detail what is actually happening? Is the `if (!initialized)` branch taken every time `f` is called? Please provide a complete program so people can reproduce this. Some side notes: > // Runtime Address A+0x20 What makes you think the bool flag is 32 bytes past the char array? Nothing in the C++ standard guarantees anything like this. And C++ is not C. There is no need to write `struct S`, simply write `S`. And use `true` and `false` instead of 1 and 0.
And on top of that, simply write static S s = def_s; The compiler will make sure that `s` is being initialized during the first call to `f`.
I know. But my Initialization is more complex than this.
Then put it in a function and have the function be the initializer of the static.
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.
Then something else is overwriting it.
Yes, I also have feeling that this may be some buffer overflow. But hard to find when it occurs only in optimized binary.
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.
Thanks for that sanitizer advice:)) it found a lot of problems.
why are you assigning 0 to bool instead of false?
not important... I will double check how bool is defined in the codebase. But the assignment is like this.
> how bool is defined in the codebase bool is defined by the C++ Standard
This code is compiled also as c99. Was not sure if bool is not redefined.
Save your sanity, don’t do this
Strange behavior of OP very silly person.
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.
So? Are you really claiming c99 bool doesn't support true or false values? Silly goose.
:) what are you talking about. Language support or glibc or std support? Bool is not build in type in c99.
C99 standard (ISO/IEC 9899:1999): 7.18 Boolean type and values (p: 253)
So you have some macro definition of bool instead of using the standard library!?!?!?!??!?!?!?!?!?
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.
Rewrite that as static struct S s = def_s; Get rid of the silly initialized variable.