Thread: typedef'd enum specifying type

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    28

    typedef'd enum specifying type

    Hi all,

    I'm having trouble setting the size of an enumerated type when it's set as a typedef:-

    Code:
    typedef enum 
    	{ MT_NULL, MT_ACK, MT_PING, MT_BYE, MT_GETINFO } 
    messageType;
    The above is a condensed version - there are more message types, but way less than 256 of them and I'd like to tell the compiler to treat this as a byte rather than the default int (I'm using GCC).

    Is there a method to do this as inline code? I have other enumerated types which are ints and work fine - I don't want to break those by setting a global compiler value.

    Many thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could try to apply __attribute__ ((packed)) to the enum declaration. I don't guarantee that it works, but it says so in the gcc info pages.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    28
    That doesn't seem to have the required effect.

    Also on further reading I've found that it is advised not to mess with ENUM sizes unless you're changing them globally (which I don't want to do).

    This is a major drawback with GCC imo and I can't believe I'm the first person to discover incompatibility issues. Almost every other compiler I ever worked with in the past has allowed the specifying of types for ENUM statements.

    Thanks for the help though.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have worked with several compilers that do not allow changing the enum size, but we've obviously used different compilers.

    I think it's quite risky (from a portability aspect) to rely on the size of enums. Instead, I would recommend something like this:
    Code:
    enum BLAH { blah, bleh, bluh ... };
    
    typedef unsigned char u8;
    
    struct somestruct
    {
       ...
       u8 someblah;
       ...
    };
    
    ...
    struct somestruct st;
    ...
    st.someblah = blah;
    This is completely valid code, and it fixes any problem without relying on compiler specific behaviour.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    28
    we've obviously used different compilers.
    Oh, I'm going back to the 1970's Of course memory usage was far more significant than it is now. If you could do something in a byte, you did it in a byte, not 4.

    Excellent suggestion though Mats - many thanks - I think I'll do something just like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enum type In class.
    By epidemic in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2007, 10:14 AM
  2. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM