home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!europa.eng.gtefsd.com!emory!gatech!usenet.ins.cwru.edu!agate!spool.mu.edu!yale.edu!ira.uka.de!slsvaat!josef!kanze
- From: kanze@us-es.sel.de (James Kanze)
- Subject: Re: setjmp/longjmp question
- In-Reply-To: steve@taumet.com's message of Tue, 26 Jan 1993 18:20:29 GMT
- Message-ID: <KANZE.93Jan28194037@slsvdnt.us-es.sel.de>
- Sender: news@us-es.sel.de
- Organization: SEL
- References: <1993Jan8.195613.14303@cadkey.com> <KANZE.93Jan25184636@slsvdnt.us-es.sel.de>
- <1993Jan26.182029.23023@taumet.com>
- Date: 28 Jan 93 19:40:37
- Lines: 46
-
- In article <1993Jan26.182029.23023@taumet.com> steve@taumet.com (Steve
- Clamage) writes:
-
- |> kanze@us-es.sel.de (James Kanze) writes:
-
- |> >The C++ standards committee have said that they are adopting the C
- |> >library more or less integrally, so I would suppose that this means
- |> >setjmp/longjmp too.
-
- |> Yes, but with a caution. If the longjmp bypasses destruction of
- |> automatic objects, the results are undefined.
-
- |> In other words, if you have C code which uses setjmp/longjmp and is
- |> otherwise legal C++ code, it will still be legal C++ code. If you
- |> add creation of auto objects which have destructors between the
- |> setjmp and the longjmp, the code no longer has defined semantics.
- |> (This could happen by calling a C++ routine from the original C code,
- |> for example.)
-
- |> The idea is to use exceptions rather than setjmp/longjmp in new code.
-
- I would love to, but my compiler doesn't agree.
-
- Just so I don't get too bad of a reputation as a hacker (what, setjmp,
- longjmp in C++), in the case I used it, I made very sure that the
- functions being called didn't use *any* variables on the stack except
- pointer variables. All real objects (with constructors and
- destructors) were allocated on the heap, and had a common base class
- which registered them in a list. When setjmp was called, everything
- in the list was deleted. Also, longjmp was never called from within
- a constructor.
-
- As a more general rule, you can emmulate exceptions with
- setjmp/longjmp, if you accept that all of your class objects derive
- from a common base class, which determines (in some machine dependent
- fashion) whether the object is on the stack or not, and if it is,
- registers it. This is definitely non-trivial, though, and in the
- presence of multiple inheritance, you'd better be sure that your
- compiler does virtual base classes right, too. (GNU definitely has
- problems, and I have heard that Borland does too.) Also, it has a
- non-negligible run-time overhead.
- --
- James Kanze email: kanze@us-es.sel.de
- GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
- Conseils en informatique industrielle --
- -- Beratung in industrieller Datenverarbeitung
-