home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
- From: tmb@arolla.idiap.ch (Thomas M. Breuel)
- Newsgroups: comp.lang.c++
- Subject: Re: Is there a way to flag errors when an automatic array is returned
- Message-ID: <TMB.92Aug25183032@arolla.idiap.ch>
- Date: 25 Aug 92 22:30:32 GMT
- References: <3499@babcock.cerc.wvu.wvnet.edu> <KENDALL.92Aug24123852@pen.centerline.com>
- <1992Aug25.135401.17891@bnr.ca>
- Sender: news@ai.mit.edu
- Reply-To: tmb@idiap.ch
- Followup-To: comp.lang.c++
- Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
- Perceptive)
- Lines: 37
- In-reply-to: holiday@bnr.ca's message of 25 Aug 92 13:54:01 GMT
-
- In article <1992Aug25.135401.17891@bnr.ca> holiday@bnr.ca (Matthew Holiday) writes:
-
- I'm not sure that it would be possible to verify at compile time that a returned
- pointer isn't a pointer to a local variable. It is possible to determine this at
- run time -- just check all returned pointers to verify that the address isn't
- within or below the stack frame about to be destroyed (you may also need to check
- that it's within the stack area, so that you don't accidentally trap on valid heap
- pointers, if the heap is below the stack in the address space, etc., etc.). It's
- probably about as expensive as run-time array bounds checks, which aren't in C or
- C++ either -- they don't fit the C/C++ design philosophy.
-
- I suspect array bounds checks are not in C++ because they would be
- virtually impossible to add in a backwards compatible fashion. At the
- root of the problem is the fact that C "pointers" are used for
- multiple purposes, including displaced arrays and locatives. In
- languages that support them, array bounds checks can usually be
- disabled locally or optimized away, so there is no great cost
- associated with them (and they are _very_ useful). I suspect that in
- the long run, emulating array bounds checks in C++ classes is going to
- be less efficient than if C++ supported them as a built-in.
-
- Checking against returning pointers to automatic variables, however,
- is very difficult in most languages. The situation is not at all
- analogous to array bounds checks. The reason is that you might also
- "return" pointers by storing them inside other data structures that
- you return, or even via global variables. The solution to this problem
- adopted by most programming languages is to use a combination of
- copying and garbage collection. Small data structures are allocated on
- the stack and copied upon return, while large data structures are
- allocated on the heap and cleaned up via a garbage collector if it
- turns out that they are no longer needed after the function returns.
-
- (Some modern language implementations have done away with the stack
- altogether, allocating even "stack frames" on the heap; the jury is
- still out on whether that is a good idea.)
-
- Thomas.
-