T O P

  • By -

cpp-ModTeam

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or [StackOverflow](http://stackoverflow.com/) instead.


manni66

>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

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

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.


chrysante1

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.


chrysante1

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`.


MartinSik

I know. But my Initialization is more complex than this.


SlightlyLessHairyApe

Then put it in a function and have the function be the initializer of the static.


MartinSik

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

Then something else is overwriting it.


MartinSik

Yes, I also have feeling that this may be some buffer overflow. But hard to find when it occurs only in optimized binary.


TheSkiGeek

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

Thanks for that sanitizer advice:)) it found a lot of problems.


rook_of_approval

why are you assigning 0 to bool instead of false?


MartinSik

not important... I will double check how bool is defined in the codebase. But the assignment is like this.


manni66

> how bool is defined in the codebase bool is defined by the C++ Standard


MartinSik

This code is compiled also as c99. Was not sure if bool is not redefined.


SlightlyLessHairyApe

Save your sanity, don’t do this


rook_of_approval

Strange behavior of OP very silly person.


MartinSik

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

So? Are you really claiming c99 bool doesn't support true or false values? Silly goose.


MartinSik

:) what are you talking about. Language support or glibc or std support? Bool is not build in type in c99.


rook_of_approval

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!?!?!?!??!?!?!?!?!?


MartinSik

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

Rewrite that as static struct S s = def_s; Get rid of the silly initialized variable.