Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quesiton with state to state10. #2

Open
Elektro93 opened this issue Mar 9, 2020 · 3 comments
Open

Quesiton with state to state10. #2

Elektro93 opened this issue Mar 9, 2020 · 3 comments

Comments

@Elektro93
Copy link

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
Copy link

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
Copy link

ttbek commented Mar 14, 2020

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
Copy link

magras commented Mar 23, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants