Re: Problems with enums across a dll interface?

From: Ben Hutchings (do-not-spam-benh_at_bwsint.com)
Date: 11/14/03


Date: 14 Nov 2003 08:11:38 -0500

SpaceCowboy wrote:
>
> I recently got into a discussion with a co-worker about using enums across
> a dll interface. He wanted to use chars instead, argueing that depending
> on compiler settings the size of an enum could change and lead to memory
> corruption. I didn't see how this was possible. He claims that if a dll
> built for a program is built with different compiler settings than the
> launcher program, the enum size could change.

This is correct. However, this is not the only thing that could change.
Be very, very wary of compiling different source files with different
compiler settings (though it's normally safe to have different
optimisation settings).

<snip>
> The only symptom of this problem is that when the launcher application is
> exited, the machine reboots. I believe it is a 98 system running VC 6.0.

So far as I know, there are no settings that would change the size of
enum objects in Visual C++, though there are in some other compilers.

Here's a list of settings for Visual C++ 6.0 that can cause
incompatibility if they differ between the various translation units and
executable modules. It might not be complete.

- preprocessor definitions and header paths, if they cause different
   definitions to be included
- pointer-to-member representation
- exception handling enabled/disabled
- RTTI enabled/disabled
- construction displacements enabled/disabled
- run-time library variant
- calling convention
- struct member alignment

Also note that different C++ compilers don't generally produce
compatible code, though the Windows C++ compiler vendors generally try
to produce code that's compatible with some version of Visual C++.

> To me this sounds more like memory being trashed by improper pointer
> management. Does any of this sound remotely possible.

Maybe. One never knows, with Windows 98. Try it out in Windows NT
(note, 2000 and XP are later versions of NT) as it is more likely to
trap such bugs.

> Is it somehow unsafe to use enums in any way? I hope not or my
> world will fall apart.

You need to be careful when adding enumerators. Strictly speaking,
after any change to shared definitions you should rebuild every file
that uses them (the One Definition Rule). In practice it's generally
safe to add enumerators as long as (1) you don't change the value of
any of the existing ones, and (2) any code that checks an enumerated
value is written to be able to cope with unrecognised values. For
example, if you have:

     enum blah
     {
         AARDVARK,
         ABACUS,
         ADVERB
     };

and you change it to be:

     enum blah
     {
         AARDVARK,
         ABACUS,
         ACTOR,
         ADVERB
     };

then the numeric value of ADVERB changes from 2 to 3. This will
break compatibility with any code compiled with the old definition.
However, if you add ACTOR at the end of the enumeration then the
existing enumerators will keep their old values, and the change
should be safe.

Note that the underlying integer type of an enumerated type
depends on the values of its members, so you must avoid adding an
enumerator that would require a change of type. So long as your
enumerators are all small enough to be represented by a char this
shouldn't be an issue.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated. First time posters: Do this! ]



Relevant Pages

  • Re: Idea for ECMA/C# Standard - compile time hash for performance
    ... I agree with you the chance of a compiler change is slim, ... and then delegating to the standard hash for fields accessed less frequently. ... or the array lookup which would require the ... > 64-bit architecture) for each enum value that doesn't map to anything. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Enum serialization
    ... so a programmer might choose to represent these as enum types. ... Just never saw enums with larger than "short" range of enumerators or ... Enumeration Compatibility ... so in serialize doc function i have in my serialize function... ...
    (microsoft.public.vc.mfc)
  • RE: passing enum value as an argument
    ... > hours after your only previous reference to Option Strict? ... indeed you would have an Enum, and you WOULD have to use the same type on the ... the compiler would force you to ... >> Public sub New ...
    (microsoft.public.dotnet.general)
  • Re: enum?
    ... It being a particular C compiler, it gave an error stating that I would have ... You are right, in all my samples using enum, is shown as you say! ... void doErrorBlink; //Function declaration ... enumeration variable of type ERROR_CODES then it would be usefull, ...
    (microsoft.public.vc.language)
  • Re: smarter enums
    ... > same scope as the enumeration they are declared in. ... The same type as the other enumerators declared in the ... > same enum declaration. ... The difference between enum declarations is ...
    (comp.lang.cpp)

Loading