https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97976

--- Comment #7 from Peter Bisroev <peter at int19h dot net> ---
Thank you for your response Jonathan.

If you have a minute, could you please clarify a few things. I have been
talking about this behavior with a few colleagues and we are all slightly
confused by the same issue. So I think the answers here can definitely help a
few people besides myself.

>It is undefined to decrement a pointer "before" the start of an object

I am sorry, but I am not sure where I am doing this? For example, lets say I am
accessing raw memory on an embedded system and I have bytes 0, 1, 1, 1 at
addresses 0, 1, 2 and 3 respectively. I know this is a trivial example and
makes no sense on x86 arch (almost, in real mode maybe). So if I call
containsBackwardsSafe(p, 2) with p == 3, shouldn't I get back -1? I guess it
all depends on what is the "object" in this context. Conceivably some other
function could have mapped that memory at that address on that system and
passed the pointer to containsBackwardsSafe() function. In that case wouldn't
the responsibility of "object" be up to the system and not the compiler?

I have also just tried going through C11 draft
(http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) to try to find
relevant standard sections that would describe this behavior. I have read
through sections:
* 6.2.5 Types (specifically paragraphs 14 and 20)
* 6.3.2.3 Pointers
* 6.5.3.1 Prefix increment and decrement operators
* 6.5.6 Additive operators (specifically paragraphs 7, 8 and 9)
* 6.5.16.2 Compound assignment

And I cannot seem to find relevant information that forbids pointer decrement
as  shown in containsBackwardsSafe() or containsBackwards() from the last
comment. If you could point me to the right section of the standard it would be
incredibly helpful. I am sure I must be missing something obvious.

Once again, thank you for your time and help on this.

Regards,
--peter

Reply via email to