Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Indexing one past the end of array? Date: Sat, 05 Oct 2019 13:13:48 -0700 Organization: None to speak of Lines: 63 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="da9954598832808a4a7535ee6b7854eb"; logging-data="880"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++oLxS/lQfC0X9Md3bVxIP" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:nPhqrjzOA2Bop4oPcUJdAP52RHc= sha1:o1hNxfVAzbuDMtpKpErJUGsIYeY= Xref: csiph.com comp.lang.c:146602 Johann 'Myrkraverk' Oskarsson writes: > Dear comp.lang.c, > > I've been reading a thread on LWN[1], notably this quote: > > > Just some pedantry, but I believe you're allowed to point to the first > > element after the end of an array, as long as you don't dereference > > that element. Pointer comparisons with such a pointer are supposed to > > work, too. > > Elsewhere, I've read that on embedded systems memory can be allocated > right up to the end of memory, and therefore such indexes are invalid > (but I cannot remember where). > > So, is it always legal to calculate one past the end of an array or > allocated memory? > > [1] https://lwn.net/Articles/278137/ Yes, it's always legal. The language guarantees it. N1570 6.5.6 "Additive operators" paragraph 8: When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. [...] Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. 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. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated. [...] A compiler needs to do whatever it has to to make this work as specified. Allocating an object at the very end of memory doesn't necessarily break this, as long as pointer addition and subtraction yield the expected results. There doesn't have to be any memory at the pointed-to location, since the pointer cannot be dereferenced. If the system requires actual memory at the location to construct the address, then the compiler needs to avoid allocating an object at the very end of addressable memory -- unless it can prove that the program never computes the address one past the last element of the object. (That only requires reserving one byte in the worst case.) For example, assume a system with 32-bit addresses and exactly 2**32 bytes of addressable memory. Assume an array object char buf[100]; is allocated starting at address 2**32-100. Then (buf+100) points just past the end of the array, and also just past the end of addressable memory. As long as evaluating (buf+100)-1 yields a valid pointer to buf[99], that's ok. (This is just an example. The standard doesn't assume this kind of mapping between addresses and integer values.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */