"Jon Harrop" <jo*@ffconsultancy.comwrote in message
news:46**********************@ptn-nntp-reader02.plus.net...
>
Is the following code invalid C:
char *a = malloc(1);
a--;
a[1] = 3;
because there is an intermediate pointer that points outside the array?
It's illegal, though it will work as you think it ought on the vast majority
of machines.
Consider a segmented architecture system and a malloc() that likes to return
blocks starting on segment boundaries. The compiler would need a special
patch to compute a - 1 correctly and then offset by one again, which is more
trouble to implement than it is worth. Therefore the standard allows it to
calculate an address pointing somewhere into the wilds, or to trap, hence
undefined behaviour.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm