home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Pointers to freed memory
- Message-ID: <1992Jul30.164035.7349@taumet.com>
- Organization: TauMetric Corporation
- References: <1992Jul29.205834.11308@hubcap.clemson.edu> <3179@dozo.and.nl>
- Date: Thu, 30 Jul 1992 16:40:35 GMT
- Lines: 35
-
- jos@and.nl (Jos Horsmeier) writes:
-
- >In article <1992Jul29.205834.11308@hubcap.clemson.edu> mjs@hubcap.clemson.edu (M. J. Saltzman) writes:
- >|I know that a pointer to freed memory should never be dereferenced,
- >|but is it standard-conforming to assign or do arithmetic with such
- >|pointers?
-
- >As long as you do not dereference such a pointer (as you indicated
- >yourself,) everything is fine. If pointer q contained a valid pointer
- >value _before_ the call to free(), it'll contain a valid pointer value
- >_after_ the call.
-
- This is not necessarily the case, although it is safe for many common
- architectures.
-
- The C Standard states that the value of the pointer after a call to free
- is 'indeterminate'. This means that just reading the value might
- produce a runtime fault.
-
- Imagine an architecture that uses a distinguished data type for pointers;
- the hardware can tell the difference between a pointer and an integer
- for example. Furthermore, there are valid and invalid values for any
- data type, such as pointers, integers, floating-point. Reading an invalid
- value causes a fault. At program start, for example, all uninitialized
- variables are given an illegal value, so that attempts to use such
- values are detected as soon as they occur. Such architectures do exist.
-
- When data is freed, the memory could be removed from the program
- address space. The pointer to the former data would now be invalid,
- and checked by the hardware on a reference. Simply testing the value
- of the pointer after the call to free could thus cause a fault.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-