Topic: which compiler is standard compliant?


Author: "Arjen van Rhijn" <rhijna@natlab.research.philips.com>
Date: Wed, 31 Oct 2001 12:04:12 GMT
Raw View
I've been debugging some code that works fine under HPUX11 but fails on
SUNOS5. I tracked it down to the following problem:
   some_type ***ppp_arr;
   ...
   *ppp_arr[index] = pp_arr2[index];

the code crashes on the assignment under SUN. When doing
  (*ppp_arr)[index] = pp_arr2[index];

the code runs fine under both platforms.

Apparantely both platform compilers interpret the first assignment statement
differently. So I was wondering which behaviour is correct according to the
standard?

thanks,
Arjen van Rhijn



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Wed, 31 Oct 2001 16:35:26 GMT
Raw View
"Arjen van Rhijn" <rhijna@natlab.research.philips.com> wrote in message
news:3bdfd7c6$0$28884$4d4ebb8e@read-nat.news.nl.uu.net...
> I've been debugging some code that works fine under HPUX11 but fails on
> SUNOS5. I tracked it down to the following problem:
>    some_type ***ppp_arr;
>    ...
>    *ppp_arr[index] = pp_arr2[index];
>
> the code crashes on the assignment under SUN. When doing
>   (*ppp_arr)[index] = pp_arr2[index];
>
> the code runs fine under both platforms.
>
> Apparantely both platform compilers interpret the first assignment
statement
> differently. So I was wondering which behaviour is correct according to
the
> standard?

*ppp_arr[index] is equivalent to *(ppp_arr[index]) since * is an unary
operator, and [] is part of a postfix expression. Here is the relevant part
of the grammar

postfix-expression:
    primary-expression
    postfix-expression [ expression ]
    ...

unary-expression:
    postfix-expression
    unary-operator cast-expression
    ...

cast-expression:
    unary-expression
    ...

Therefore ppp_arr[index] is a postfix-expression, and hence also a
unary-expression and a cast-expression, so * can be applied "unary-operator
cast-expression".

However, *ppp_arr is an unary-expression, which is not a postfix-expression,
so [] cannot be applied.

(*ppp_arr)[index] is different, because the brackets make (*ppp_arr) a
primary expression.

Anyway, if (*ppp_arr)[index] is what was intended, the Sun Compiler is
correct, as that is not what was written.

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 31 Oct 2001 20:20:43 GMT
Raw View
Arjen van Rhijn wrote:
>
> I've been debugging some code that works fine under HPUX11 but fails on
> SUNOS5. I tracked it down to the following problem:
>    some_type ***ppp_arr;
>    ...
>    *ppp_arr[index] = pp_arr2[index];

which is equivalent to:

 *(ppp_arr[index]) = pp_arr2[index];

The LHS is equivalent to *(ppp_arr[index]), or in other forms,
*(*(ppp_arr+index)), or ppp_arr[index][0]

> the code crashes on the assignment under SUN. When doing
>   (*ppp_arr)[index] = pp_arr2[index];

In this case, the LHS is equivalent to *((*ppp_arr)+index) or
ppp_arr[0][index].

> the code runs fine under both platforms.
>
> Apparantely both platform compilers interpret the first assignment statement
> differently. So I was wondering which behaviour is correct according to the
> standard?

That depends entirely upon what you've put in ppp_arr, and what value
'index' has.

The first form requires that ppp_arr must contain a pointer to an array
of at least 'index-1' pointers to pointers, and that ppp_arr[index] must
contain a pointer to an array containing at least one pointer.
Otherwise, the behavior is undefined.

The second form requires that ppp_arr must contain a pointer to an array
contain at least 1 pointer to a pointer. It also requires that
ppp_arr[0] must contain a pointer to an array of at least 'index-1'
pointers. Otherwise, the behavior is undefined.

Note: wherever I say that a pointer must refer to an array of a given
length, if that length happens to be 1, then a pointer to a single
object of the appropriate type is also acceptable.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Steve Clamage <clamage@eng.sun.com>
Date: Thu, 1 Nov 2001 15:10:53 GMT
Raw View
On Wed, 31 Oct 2001, Arjen van Rhijn wrote:
>
> I've been debugging some code that works fine under HPUX11 but fails on
> SUNOS5. I tracked it down to the following problem:
>    some_type ***ppp_arr;
>    ...
>    *ppp_arr[index] = pp_arr2[index];
>
> the code crashes on the assignment under SUN. When doing
>   (*ppp_arr)[index] = pp_arr2[index];
>
> the code runs fine under both platforms.


The two versions of the assignment statement mean different things.

The first one is equivalent to
 some_type** temp = ppp_arr[index];
 *temp = pp_arr2[index];

The second one is equivalent to
 some_type** temp = ppp_arr[0];
 temp[index] = pp_arr2[index];

One of these interpretations probably is nonsense, assigning through
a pointer with a garbage value. What happens after that will depend
on implementation details, but you can't expect any particular result.

--
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]