lefticus / cpp_weekly_game_project

The Unlicense
107 stars 22 forks source link

Quesiton with state to state10. #2

Open Elektro93 opened 4 years ago

Elektro93 commented 4 years ago

Hello, I am coming from embedded C, I just started learning C++, so maybe this question is going far ahead or even sound stupid to veteran C++ devs. I know that the purpose of the checkbox is to see the progress of the weekly episodes so it is a pretty simple solution to do that but, if we would like to optimize the solution, does the following make sense: If I understand properly the bool type is taking up a byte in the memory. If we would like to optimize from the side of memory, would it be better to use bit fields(i am not sure if this is the way to go with C++20), or bitsets/dynamic_bitsets. But yes this would have to be casted into a bool. Example: typedef struct t { uint16_t State:1; uint16_t State1:1; uint16_t State2:1; uint16_t State3:1; uint16_t State4:1; uint16_t State5:1; uint16_t State6:1; uint16_t State7:1; uint16_t State8:1; uint16_t State9:1; uint16_t State10:1; }CheckBox_Status;

Thank you for doing this tutorial series.

Organic-Code commented 4 years ago

I don’t really think that’s really relevant in this use case, I mean, sure you might be saving a bit of memory, but that’s on pretty temporary code (I believe), and that’s really not much on a desktop PC.

However, there is a much more important reason why you don’t want to do that : since ImGui will be taking your parameter by pointer (and assumes a bool*), it’ll set a byte anyway instead of a bit, corrupting the state left and right (well, technically maybe not, since it’s UB). You could work around it and convert to an actual bool before and after each call to ImGui, but I believe it’s not really worth the effort

ttbek commented 4 years ago

No need for optimizing that. I get that coming from embedded it might look fast and loose, but it won't be worthwhile to optimize everything as we go along. Later on when there is a more significant code base, then we can profile and see which things really need optimizing. This isn't an excuse for poor architecture, but yeah, even I thought it would just even look cleaner and be faster to write an array (C array, nothing fancy) than typing them all out. Your understanding is correct though, if we were indeed out to optimize memory then that would potentially be a way to go.

magras commented 4 years ago

If you want a collection of flags there is std::bitset. But @Organic-Code's point about impossibility of taking an address of bitset elements still applies.