• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Specifying "enum" data size
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Specifying "enum" data size


  • Subject: Re: Specifying "enum" data size
  • From: Rush Manbert <email@hidden>
  • Date: Mon, 26 Sep 2005 17:00:03 -0700

Mark Wagner wrote:

On 9/24/05, Andreas Grosam <email@hidden> wrote:


On 24.09.2005, at 02:17, Chris Espinosa wrote:



On Sep 23, 2005, at 5:04 PM, Mark Wagner wrote:



How do I specify the size of an "enum" value? I've got a program that
links to a number of static libraries. In one of them, an "enum" for
the typedef'd "BoolEnum" type is one bit, while in another, it's 32
bits. Needless to say, this causes problems when passing structs
containing BoolEnums back and forth.

Does GCC recognize the "#pragma enumsalwaysint" pragma? Might the
presence of this in some of the source files be causing problems?

I'm using XCode 1.5 and GCC 3.3, and I'm writing in C.


The only control available is -fshort-enums ("Short Enumeration
Constants") which you can apply on a target or on a single file.
There's no pragma to make this apply only to specific enums within a
file. This will make enums as small as possible for the range of
enums given.

Notice that Mac OS X frameworks often declare structs that contain
enumerated values, so changing the size of enums might make your code
unable to call Mac OS X system functions.


Are you sure? I thought, enums were only be used to declare constants
and never be used as types. Thus, enums do not have any effect on the
layout or sizeof of structs.

Otherwise, in the C language this would be a very fragile usage. In C,
i would never ever use enums as types in interfaces since the size of
the enum type is compiler "implementation defined", may depend on
optimization flags, architecture, etc.

Note, that binary compatibility (here the size of enums) may only be
guaranteed when you use the same C compiler.
I also wouldn't use the switch for enums used in interfaces. The
documentation says this:
"Warning: the -fshort-enums switch causes GCC to generate code that is
not binary compatible with code generated without that switch. Use it
to conform to a non-default application binary interface. "



In your case, it looks like, that your static library has been compiled
with this switch on, or with another compiler, or other compiler flags
changing the size of the enum - thus it is not binary compatible
anymore.

Well, to workaround your problem, re-compile the static libraries using
*your* compiler (gcc 3.3) in order to get ABI compatibility.



Everything was compiled using gcc 3.3, with the -fshort-enums switch disabled. That's why I'm asking about "#pragma enumsalwaysint" -- it's the only thing I can think of that would be causing the problems.

For the Apple ABI, how long should an enum type with two possible
values be, anyway?  Is the library that thinks it's 32 bits correct,
or is the library that thinks it's one bit correct?  Or are they both
wrong?



I just double checked this to be sure, and I'm just going to quote from Stroustrup:
"The sizeof an enumeration is the sizeof some integral type that can hold its range and not larger than sizeof(int), unless an enumerator cannot be represented as an int or an unsigned int. For example, sizeof(e1) could be 1 or maybe 4 but not 8 on a machine where sizeof(int)==4."


I think that if you want to force the minimum size, you will need a "dummy" value that you specify as something like:
dummyEnumVal = 0xffff, which should force sizeof the enum to be 2 (16 bits). It sounds like the compiler that is using a single bit is compliant, while the one that uses 32 bits has just defaulted to using sizeof(int), and really doesn't comply. In this case, it sounds like you might need to specify a dummy value as 0xffffffff, so that the enum size is 32 bits on each machine. Of course, then you might have endian issues to deal with... :-)


Hope it helps,
Rush
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Specifying "enum" data size
      • From: Andreas Grosam <email@hidden>
References: 
 >Specifying "enum" data size (From: Mark Wagner <email@hidden>)
 >Re: Specifying "enum" data size (From: Chris Espinosa <email@hidden>)
 >Re: Specifying "enum" data size (From: Andreas Grosam <email@hidden>)
 >Re: Specifying "enum" data size (From: Mark Wagner <email@hidden>)

  • Prev by Date: Re: Debug/Release Library Matching
  • Next by Date: Re: BugReport: Build after changing build config compiles old code
  • Previous by thread: Re: Specifying "enum" data size
  • Next by thread: Re: Specifying "enum" data size
  • Index(es):
    • Date
    • Thread