home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 20060 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.7 KB  |  60 lines

  1. Newsgroups: comp.lang.c++
  2. 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
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: setjmp/longjmp question
  5. In-Reply-To: steve@taumet.com's message of Tue, 26 Jan 1993 18:20:29 GMT
  6. Message-ID: <KANZE.93Jan28194037@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <1993Jan8.195613.14303@cadkey.com> <KANZE.93Jan25184636@slsvdnt.us-es.sel.de>
  10.     <1993Jan26.182029.23023@taumet.com>
  11. Date: 28 Jan 93 19:40:37
  12. Lines: 46
  13.  
  14. In article <1993Jan26.182029.23023@taumet.com> steve@taumet.com (Steve
  15. Clamage) writes:
  16.  
  17. |> kanze@us-es.sel.de (James Kanze) writes:
  18.  
  19. |> >The C++ standards committee have said that they are adopting the C
  20. |> >library more or less integrally, so I would suppose that this means
  21. |> >setjmp/longjmp too.
  22.  
  23. |> Yes, but with a caution.  If the longjmp bypasses destruction of 
  24. |> automatic objects, the results are undefined.
  25.  
  26. |> In other words, if you have C code which uses setjmp/longjmp and is
  27. |> otherwise legal C++ code, it will still be legal C++ code.  If you
  28. |> add creation of auto objects which have destructors between the
  29. |> setjmp and the longjmp, the code no longer has defined semantics.
  30. |> (This could happen by calling a C++ routine from the original C code,
  31. |> for example.)
  32.  
  33. |> The idea is to use exceptions rather than setjmp/longjmp in new code.
  34.  
  35. I would love to, but my compiler doesn't agree.
  36.  
  37. Just so I don't get too bad of a reputation as a hacker (what, setjmp,
  38. longjmp in C++), in the case I used it, I made very sure that the
  39. functions being called didn't use *any* variables on the stack except
  40. pointer variables.  All real objects (with constructors and
  41. destructors) were allocated on the heap, and had a common base class
  42. which registered them in a list.  When setjmp was called, everything
  43. in the list was deleted.  Also, longjmp was never called from within
  44. a constructor.
  45.  
  46. As a more general rule, you can emmulate exceptions with
  47. setjmp/longjmp, if you accept that all of your class objects derive
  48. from a common base class, which determines (in some machine dependent
  49. fashion) whether the object is on the stack or not, and if it is,
  50. registers it.  This is definitely non-trivial, though, and in the
  51. presence of multiple inheritance, you'd better be sure that your
  52. compiler does virtual base classes right, too.  (GNU definitely has
  53. problems, and I have heard that Borland does too.)  Also, it has a
  54. non-negligible run-time overhead.
  55. --
  56. James Kanze                             email: kanze@us-es.sel.de
  57. GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
  58. Conseils en informatique industrielle --
  59.                    -- Beratung in industrieller Datenverarbeitung
  60.