CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: size of enum

  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Angry size of enum

    Hi,
    When I create an enum like this
    enum x {aaa,bbb,ccc};
    cout<<sizeof(x)<<endl; shows the 4

    if I put the enum in a class like this
    class abc{
    public:
    enum a { aaa,bbb,ccc=102,dd};

    };

    and in the main I Print tha size of the object it should show me 4 again but it is showing 1

    I think this is wrong. I am using .NET compiler

    plz clarify

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: size of enum

    Your class has no data members, thus the size of any object of this class should theoretically be zero. However, C++ demands that objects have a size of at least 1, therefore 1 is what you get as a result.

    abc::a is a type, not a data member in case you wonder.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Jul 2007
    Posts
    249

    Re: size of enum

    but I have given index for one of them i.e for ccc.
    Actually I have not used much enum and not clear with the concept
    can you please explain some other related type as of enum so that if we declare it in a class then the size of the object becomes 1 (other than enum)

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: size of enum

    Here is another example:

    You have Outer class with no members (empty), but it has inner type defined (GiganticInner). However, as you can see, it has no impact on size of outer class objects, as it has no data fields:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Outer {
    
    	class GiganticInner {
    		int array[100000];
    	};
    
    public:
    	void printInnerSize() {
    		GiganticInner temp;
    		cout << "Size of object of inner class: " << sizeof(temp) << '\n';
    	}
    };
    
    
    int main() {
    
    	Outer outer;
    	cout << "Size of object of outer class: " << sizeof(outer) << '\n';
    	outer.printInnerSize();
    }

    My Outer is like your abc, and GiganticInner just like internal enym type definition.

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    Jul 2007
    Posts
    249

    Re: size of enum

    Thanks a lot
    Now I am very much clear

  6. #6
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: size of enum

    Enums are just a collection constant integers.
    Remember that enums are numbered starting with 0 unless you specify a number.

    so:
    Code:
    enum a { aaa,bbb,ccc=102,dd}
    is basically like doing:
    Code:
    #define aaa 0
    #define bbb 1
    #define ccc 102
    #define dd  103
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: size of enum

    And another compiler may well make the the size of that enum 1 or even 2, since it only has to be big enough to hold the largest value, and yours would fit in a char.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured