0

Say I have this class and am happy with leaving all the non-bit field members with their garbage contents, but I need to set all the bit fields to 0. What is the most efficient way to achieve that? For instance, sould you expect an -O2 build of this would inline the memset() to be more efficient than an initializer list of bf1(0),bf2(0),...bf400(0)? Or at least same speed? Or is there another obvious way to do this?

Also if in addition I had 100 std::string is there any way to keep them from all initializing and just left as garbage values?

class Big {
Big() {
  memset( this + offsetof( this, psz300 ) + sizeof( psz300 ), '\0',
          sizeof( this ) - offsetof( this, psz300 ) - sizeof( psz300 ) );
};

double d1, ... d100;
int    i1, ... i100;
const char* psz1, ... * psz300;

unsigned long bf1:2,  ... bf400:2;
}

;

Swiss Frank
  • 1,985
  • 15
  • 33
  • 2
    You cannot "leave std::string as garbage values". This is just not possible. It's not how C++ works. I suggest getting a good book on C++. Here is the [recommended list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – n. m. could be an AI Oct 04 '20 at 13:15
  • As always there are no certain answers to 'which is more efficient' questions. The only thing you can do is try both methods and time them, or examine the generated machine code. And of course remember that any results you find are true only for your current situation. Another compiler, another platform and the results might be different. – john Oct 04 '20 at 15:05

0 Answers0