Article View: comp.std.c
Article #181Re: Intermediate addr outside object: legal?
From: John F. Woods
Date: Mon, 06 Apr 1992 16:16
Date: Mon, 06 Apr 1992 16:16
25 lines
1565 bytes
1565 bytes
darcy@druid.uucp (D'Arcy J.M. Cain) writes: >In fact I don't think the standard even forbids "fn2(foo[-1000])" >although your OS may complain (core dump) when you reference the data. It is "forbidden": In the section on Additive Operators, discussing the Semantics of pointer arithmetic: "If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined." >> } foo [10000]; >> #define bar (foo - 8000) >> fn2(bar[9000]); /* <---- */ >Chapter 3.3.2.1 says that E1[E2] is identical to "(*(E1+(E2)))." After >expanding macros the above becomes "(foo - 8000)[9000]" which is >identical to "(*((foo - 8000)+(9000)))." Removing superfluous parens >that gives "*(foo - 8000 + 9000)" or "*(foo + 1000)" which looks >perfectly legitimate to me. The C abstract machine must perform the calculations as written in the order specified by the syntax (3.3 Expressions, see footnote 35). Implementations are not obligated to simplify expressions when they can. This expression asks for the calculation of "foo - 8000" as an intermediate result, which is illegal.
Technical Details
Message-ID:
<11214@ksr.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!world!ksr!jfwReferences:
<2160001@aspen.IAG.HP.COM> <1992Apr4.095414.8594@druid.uucp>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!world!ksr!jfw From: jfw@ksr.com (John F. Woods) Newsgroups: comp.std.c Subject: Re: Intermediate addr outside object: legal? Message-ID: <11214@ksr.com> Date: 6 Apr 92 12:16:35 EDT References: <2160001@aspen.IAG.HP.COM> <1992Apr4.095414.8594@druid.uucp> Sender: news@ksr.com Lines: 25