typedef enum enumStruc_2 {     ENUM_5 = 0xFFFFFFF0, ENUM_6, ENUM_7, ENUM_8 } enumStruc_2;   Here, size of enum is 4 bytes(Integer size). How it saves all of its enum member values within 4 bytes? Is always enum size 4 bytes irrespective of value of its member?
73828
I'm creating a set of enum values, but I need each enum value to be 64 bits wide. If I recall correctly, an enum is generally the same size as an int; but I thought I read somewhere that (at least in GCC) the compiler can make the enum any width they need to be to hold their values. So, is it possible to have an enum that is 64 bits wide?
114469
I was wondering what could be the size of an object of an empty class. It surely could not be 0 bytes since it should be possible to reference and point to it like any other object. But, how big is such an object?  I used this small program:  #include &lt;iostream&gt; using namespace std;  class Empty {};  int main() {     Empty e;     cerr &lt;&lt; sizeof(e) &lt;&lt; endl;     return 0; }   The output I got on both Visual C++ and Cygwin-g++ compilers was 1 byte! This was a little surprising to me since I was expecting it to be of the size of the machine word (32 bits or 4 bytes).  Can anyone explain why the size of 1 byte? Why not 4 bytes? Is this dependent on compiler or the machine too? Also, can someone give a more cogent reason for why an empty class object will not be of size 0 bytes?
994
Recently saw someone commending another user on their use of sizeof var instead of sizeof(type). I always thought that was just a style choice. Is there any significant difference? As an example, the lines with f and ff were considered better than the lines with g and gg:   typedef struct _foo {} foo;   foo *f = malloc(count * sizeof f);  foo *g = malloc(sizeof(foo) * count);   foo **ff = malloc(count * sizeof *ff);  foo **gg = malloc(sizeof(foo*) * count);   In my opinion, the first set is just a matter of style. But in the second pair of lines, the extra second * can easily be confused for a multiplication.
80355
I have this struct:  struct Map {     public int Size;      public Map ( int size )     {         this.Size = size;     }      public override string ToString ( )     {         return String.Format ( "Size: {0}", this.Size );     } }   When using array, it works:  Map [ ] arr = new Map [ 4 ] {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  arr [ 2 ].Size = 0;   But when using List, it doesn't compile:  List&lt;Map&gt; list = new List&lt;Map&gt; ( ) {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  list [ 2 ].Size = 0;   Why?
84371
In ANSI C, offsetof is defined as below.  #define offsetof(st, m) \     ((size_t) ( (char *)&amp;((st *)(0))-&gt;m - (char *)0 ))   Why won't this throw a segmentation fault since we are dereferencing a NULL pointer? Or is this some sort of compiler hack where it sees that only address of the offset is taken out, so it statically calculates the address without actually dereferencing it? Also is this code portable?
40007
I've seen documentation whereby an \alph command is put around the \begin{enumerate} somewhere, but I'm not entirely sure how that operates...
59878
I want to pass an enum value as command parameter in WPF, using something like this:  &lt;Button      x:Name="uxSearchButton"      Command="{Binding Path=SearchMembersCommand}"      CommandParameter="SearchPageType.First"     Content="Search"&gt; &lt;/Button&gt;   SearchPageType is an enum and this is to know from which button search command is invoked.  Is this possible in WPF, or how can you pass an enum value as command parameter?
49609
I have a bunch of enum types in some library header files that I'm using, and I want to have a way of converting enum values to user strings - and vice-versa.   RTTI won't do it for me, because the 'user strings' need to be a bit more readable than the enumerations.  A brute force solution would be a bunch of functions like this, but I feel that's a bit too C-like.  enum MyEnum {VAL1, VAL2,VAL3};  String getStringFromEnum(MyEnum e) {   switch e   {   case VAL1: return "Value 1";   case VAL2: return "Value 2";   case VAL1: return "Value 3";   default: throw Exception("Bad MyEnum");   } }   I have a gut feeling that there's an elegant solution using templates, but I can't quite get my head round it yet.  UPDATE: Thanks for suggestions - I should have made clear that the enums are defined in a third-party library header, so I don't want to have to change the definition of them.  My gut feeling now is to avoid templates and do something like this:  char * MyGetValue(int v, char *tmp); // implementation is trivial  #define ENUM_MAP(type, strings) char * getStringValue(const type &amp;T) \  { \  return MyGetValue((int)T, strings); \  }  ; enum eee {AA,BB,CC}; - exists in library header file  ; enum fff {DD,GG,HH};   ENUM_MAP(eee,"AA|BB|CC") ENUM_MAP(fff,"DD|GG|HH")  // To use...      eee e;     fff f;     std::cout&lt;&lt; getStringValue(e);     std::cout&lt;&lt; getStringValue(f);
42535
If I have a class as follows     class Example_Class     {        private:          int x;           int y;         public:           Example_Class()           {               x = 8;              y = 9;          }        ~Example_Class()         { }     };   And a struct as follows  struct {    int x;    int y; } example_struct;   Is the structure in memory of the  example_struct simmilar to that in Example_Class  for example if I do the following  struct example_struct foo_struct; Example_Class foo_class = Example_Class();  memcpy(&amp;foo_struct, &amp;foo_class, sizeof(foo_struct));   will foo_struct.x = 8 and foo_struct.y = 9 (ie: the same values as the x,y values in the foo_class) ?  The reason I ask is I have a C++ library (don't want to change it) that is sharing an object with C code and I want to use a struct to represent the object coming from the C++ library. I'm only interested in the attributes of the object.  I know the ideal situation would be to have Example_class wrap arround a common structure between the C and C++ code but it is not going to be easy to change the C++ library in use.
96976
Is there a way to have a 64 bit enum in C++?  Whilst refactoring some code I came across bunch of #defines which would be better as an enum, but being greater than 32 bit causes the compiler to error.  For some reason I thought the following might work:  enum MY_ENUM : unsigned __int64   {       LARGE_VALUE = 0x1000000000000000,   };
87194
I've been told that code such as:  for (int i = 0; i &lt; x.length(); i++) {     // blah }   is actually O(n^2) because of the repeated calls to x.length(). Instead I should use:  int l = x.length(); for (int i = 0; i &lt; l; i++) {     // blah }   Is this true? Is string length stored as a private integer attribute of the String class? Or does String.length() really walk the whole string just to determine its length?
59948
I originally had an array[1..1000] that was defined as a global variable. But now I need that to be n, not 1000 and I don't find out n until later. I know what n is before I fill the array up but I need it to be global therefore need a way to define the size of a global array at run time.  Context is filling an array with a linear transformation of the bytes in a file.  I don't know how big the file is until someone wants to open it and the files can be of any size.
81643
What is the purpose of the LongLength property for arrays in .Net.  Using a standard integer for length, you could accommodate up to 2 billion indices.  Are there really people using .Net to maintain a single array with more the 2 billion elements.  Even if each element was a single byte, that would still be 2 GB of data.   Is it feasible to use such a large array in .Net?
66268
In a C++ project I'm working on, I have a flag kind of value which can have four values. Those four flags can be combined. Flags describe the records in database and can be:   new record deleted record modified record existing record   Now, for each record I wish to keep this attribute, so I could use an enum:  enum { xNew, xDeleted, xModified, xExisting }   However, in other places in code, I need to select which records are to be visible to the user, so I'd like to be able to pass that as a single parameter, like:   showRecords(xNew | xDeleted);   So, it seems I have three possible appoaches:  #define X_NEW      0x01 #define X_DELETED  0x02 #define X_MODIFIED 0x04 #define X_EXISTING 0x08   or  typedef enum { xNew = 1, xDeleted, xModified = 4, xExisting = 8 } RecordType;   or   namespace RecordType {     static const uint8 xNew = 1;     static const uint8 xDeleted = 2;     static const uint8 xModified = 4;     static const uint8 xExisting = 8; }   Space requirements are important (byte vs int) but not crucial. With defines I lose type safety, and with enum I lose some space (integers) and probably have to cast when I want to do a bitwise operation. With const I think I also lose type safety since a random uint8 could get in by mistake.  Is there some other cleaner way?  If not, what would you use and why?  P.S. The rest of the code is rather clean modern C++ without #defines, and I have used namespaces and templates in few spaces, so those aren't out of question either.
81127
Would the following make the objects fulfill all charasteristics that enums have in JavaScript? Something like: my.namespace.ColorEnum = {     RED : 0,     GREEN : 1,     BLUE : 2 }  // later on  if(currentColor == my.namespace.ColorEnum.RED) {    // whatever }  Or is there some other way I can do this?
93814
A friend &amp; I were debating how Inf's and NaN's are stored during lunch today.  Take Fortran 90 for example.  4-byte reals can obtain the value of Inf or NaN.  How is this stored internally?  Presumably, a 4-byte real is a number represented internally by a 32 digit binary number.  Are Inf's and NaN's stored as 33 bit binary numbers?
1080
How can you enumerate an enum in C#?  E.g. the following code does not compile:  public enum Suit {     Spades,     Hearts,     Clubs,     Diamonds }  public void EnumerateAllSuitsDemoMethod() {     foreach (Suit suit in Suit)     {         DoSomething(suit);     } }   And it gives the following compile-time error:     'Suit' is a 'type' but is used like a 'variable'   It fails on the Suit keyword, the second one.