enum

This is a discussion on enum within the C++ Programming forums, part of the General Programming Boards category; I am wondering how many bits (in memory) are used for a declared enum type? e.g. int is 32 bits ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    enum

    I am wondering how many bits (in memory) are used for a declared enum type?

    e.g. int is 32 bits
    bool is 1 bit
    enum??

    At enum, is it dependent on how many values it can take, e.g.
    { true, false} would take 1 bit,
    {one, two, three} 2 bits
    {1,2,...,n } would take log(2)n bits.
    etc.

    ??

    I have to make a huge array. Each element must contain a certain parameter, and I can use either an enum type with three values or two bool variables.
    I was wondering whether it would be better to use two bool variables or one enum with 3 values?

    Thank you!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Enum's are basically just ints:
    Code:
    enum data {one,two,three};
    data test;
    cout << sizeof(test);
    Outputs "4" (4 bytes, 32 bits) on my machine.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    They might not be, though. The standard only requires them to be large enough to hold all values. The data enum, the highest value being 2, might be anything from 1 to 8 bytes long. All DirectX enums have for that reason a member called FORCE_DWORD (or some variant) that has the value 0xffffffff and forces 4 bytes size.

    bool isn't 1 bit large btw, it's usually 1, 2 or 4 bytes large.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by CornedBee
    They might not be, though. The standard only requires them to be large enough to hold all values. The data enum, the highest value being 2, might be anything from 1 to 8 bytes long.
    According to my copy of the standard (C89):
    Each enumerated type shall be compatible with an integer type: the choice of type is implementation-defined."
    IOW, the compiler may define a special type for an enum, but will ultimately be an int or int look-alike.

    I'm not certain if the C-99 or C++ standard has redefined this or not, but I doubt it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    char and short are integer types too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    yes .. i think that enum as int takes which is 4 bytes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 09:16 PM
  2. enum [tag] [: type] Is this non-standard?
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2007, 09:29 PM
  3. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 12:16 PM
  4. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  5. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21