|
From: Erik C. <er...@ar...> - 2003-09-29 18:38:02
|
On Mon, Sep 29, 2003 at 06:53:12PM +0000, Philippe Elie wrote:
> int main(void)
> {
> char * p1 = malloc(1000);
> char * p2 = malloc(1000);
> printf("%p %p %p\n", p1, p2, &p1[p2 - p1]);
>
> p1[p2 - p1 + 1] = 0; /* expect error here */
This isn't actually legal C code, and nor is the line above it.
It's section 6.3.6 in the ANSI standard, some other number in the
ISO standard. I'm looking at the Herbert Schildt book "The Annotated
C Standard".
You are only allowed to subtract pointers if they are pointers
into the same array object.
So it's a bit dodgy, though of course it is legal gcc, just not
legal C. And it could be legal some-other-language-that-you-compile-
and-feed-to-valgrind.
--
Erik Corry er...@ar...
A: Because it messes up the order in which people normally read text.
Q: Why is top-replying such a bad thing?
A: Top-replying.
Q: What is the most annoying thing in email?
|