Re: Problems with enums across a dll interface?
From: Ben Hutchings (do-not-spam-benh_at_bwsint.com)
Date: 11/14/03
- Next message: Karl Heinz Buchegger: "Re: static_cast<unsigned int> question..."
- Previous message: Ekkehard Morgenstern: "Re: c++ standard"
- In reply to: SpaceCowboy: "Problems with enums across a dll interface?"
- Next in thread: Martin Slater: "Re: Problems with enums across a dll interface?"
- Reply: Martin Slater: "Re: Problems with enums across a dll interface?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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! ]
- Next message: Karl Heinz Buchegger: "Re: static_cast<unsigned int> question..."
- Previous message: Ekkehard Morgenstern: "Re: c++ standard"
- In reply to: SpaceCowboy: "Problems with enums across a dll interface?"
- Next in thread: Martin Slater: "Re: Problems with enums across a dll interface?"
- Reply: Martin Slater: "Re: Problems with enums across a dll interface?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|