Re: C++ Runtime Binding

From: Igor Tandetnik (itandetnik_at_mvps.org)
Date: 03/16/04


Date: Tue, 16 Mar 2004 09:29:45 -0500


"Ben Rush" <kwendex@yahoo.com> wrote in message
news:OIIXQjvCEHA.1028@TK2MSFTNGP11.phx.gbl...
> What you're saying is that when the CRT is linked, it is the outside
force
> that calls the constructors and destructors of global/static objects.
> Whereas in all other circumstances, the call to an object's
constructor and
> destructor is emitted at compile time.

Well, calls to constructors and destructors are emitted at compile time
in all cases. What happens is, for each global object the compiler emits
a plain C function that calls this object's contstructor, and another
one for destructor. Then the compiler and linker conspire to build a
global array of function pointers initialized to refer to all
constructor functions. The start-up code (be it in CRT or ATL7) simply
walks this array and calls each function in turn. As the last step, each
constructor function calls atexit() to register a corresponding
destructor function to run at shut-down. Again, ATL7 emulates enough of
the CRT to make atexit() work.

> I understand, unless I'm under another misconception, that due to the
> multiphase construction of ATL objects one shouldn't (and possibly
can't)
> call new on an ATL object, however are new and delete defined for
plain,
> vanilla C++ classes if I choose to not link with the CRT, then?

I don't understand what you are talking about, sorry.

> I'm assuming
> new and delete are defined by the CRT.

::operator new and ::operator delete (the two memory allocation
functions, roughly equivalent to malloc and free) are defined by the CRT
and emulated by both ATL3 and ATL7 in MinCRT mode. Note that these do
_not_ call constructors or destructors - they just allocate memory.

> Do I need to implement these
> operators by wrapping calls to HeapAlloc and HeapFree?

No you don't. It just works.

-- 
With best wishes,
    Igor Tandetnik
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Relevant Pages

  • Re: C++ Runtime Binding
    ... What you're saying is that when the CRT is linked, ... that calls the constructors and destructors of global/static objects. ... Notice Igor carefully referred to global/static objects' constructors/ ...
    (microsoft.public.vc.atl)
  • Re: C++ Runtime Binding
    ... I believe you are under the misconception that constructors and ... destructors only, since these are the only ones affected in ATL3. ... > "Igor Tandetnik" wrote in message ... >>> called without the CRT? ...
    (microsoft.public.vc.atl)
  • Re: C++ Runtime Binding
    ... I guess what I should have asked is how are the constructors and destructors ... called without the CRT? ... My understanding, and it could be quite easily ...
    (microsoft.public.vc.atl)
  • Re: C++ Runtime Binding
    ... > called without the CRT? ... > flawed, is that without the startup code for the runtime, constructors ... ATL7 essentially replicates the necessary start-up code - just enough of ... it to run constructors and destructors of global objects. ...
    (microsoft.public.vc.atl)
  • Re: global and static object
    ... C++ global constructors are placed in the same list of ... so the CRT runs those without knowing whether ... it is construction of a C++ object or initialization of a C variable. ... whether those functions call C++ constructors or not. ...
    (microsoft.public.vc.language)