home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!sdd.hp.com!swrinde!gatech!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Newsgroups: comp.lang.c
- Subject: Pointers outside of array boundary
- Summary: Elaboration on FAQ answer wanted
- Message-ID: <1992Jul28.180945.22332@hubcap.clemson.edu>
- Date: 28 Jul 92 18:09:45 GMT
- Organization: Clemson University, Clemson SC
- Lines: 31
-
- I am having an e-mail conversation with someone regarding the
- _Numerical_Recipes_in_C_ practice of creating arrays with offset
- indices. The sample code in question is
-
- float x = malloc(10*sizeof(float));
- --x;
-
- which is supposed to give an array whose elements are addressed
- as x[1]..x[10]. The FAQ points out that this is not strictly
- conforming, since the decremented value of x no longer points
- to a legitimate element of the allocated array. My interlocutor
- asks what could possibly go wrong if we only ever dereference
- x[1]..x[10], and I can only respond with hypothetical arguments
- about the possibility of generating an invalid address.
-
- My question is: Can anyone name an existing machine on which this code
- could fail, or give a really convincing hypothetical argument why it
- is a bad idea? My interlocutor claims the criticism of this form is
- legalistic, and not based on practical considerations.
-
- Also, could someone quote the relevant part of the standard? The
- relevant portions of K&R2 cited in the FAQ don't seem to quite address
- the issue head-on.
-
- Thanks in advance.
-
-
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-