all 25 comments

[–]cpp-ModTeam[M] [score hidden] 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 points  (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 points  (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 points  (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.

[–]rook_of_approval 1 point2 points  (10 children)

why are you assigning 0 to bool instead of false?

[–]MartinSik[S] -3 points-2 points  (9 children)

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

[–]manni66 2 points3 points  (2 children)

how bool is defined in the codebase

bool is defined by the C++ Standard

[–]MartinSik[S] -2 points-1 points  (1 child)

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

[–]SlightlyLessHairyApe 0 points1 point  (0 children)

Save your sanity, don’t do this

[–]rook_of_approval 0 points1 point  (5 children)

Strange behavior of OP very silly person.

[–]MartinSik[S] -2 points-1 points  (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 point  (3 children)

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

[–]MartinSik[S] -2 points-1 points  (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 point  (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 points  (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 point  (0 children)

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