Re: Program crashes when running it outside dev environment



Richard Tobin said:

In article <Vuadnbvvi6tuTPTbnZ2dnUVZ8vydnZ2d@xxxxxx>,
Richard Heathfield <rjh@xxxxxxxxxxxxxxx> wrote:

No, there are (rare) occasions when you do need a cast. But I don't
know of any occasion where you *need* a cast AND omitting it violates
a constraint or constitutes a syntax error.

I must be misunderstanding you...

struct foo {int id; ...};

/* comparison function for qsort() */
int compar(void *a, void *b)
{
return ((struct foo *)a)->id - ((struct foo *)b)->id;
}

Omitting the casts here would be a constraint violation.

In some respects, that's a well-constructed example. (In other respects,
not so good.) I'm afraid my only answer is that I wouldn't write it
like that, because it's broken in at least two ways. I'd write it like
this:

int compar(const void *vp1, const void *vp2)
{
const struct foo *p1 = vp1;
const struct foo *p2 = vp2;
return (p1->id > p2->id) - (p1->id < p2->id);
}

My version, unlike yours, is compatible with qsort. My version, unlike
yours, doesn't risk overflow. And my version, unlike yours, doesn't use
casts, and yet there is no constraint violation or syntax error.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Re: int pointing to char
    ... >> outside of a constraint is violated, ... If a constraint violation implied undefined ... Instead, the standard clearly draws a line, as if to say ... constraint violation also violates syntax or violates semantics does the ...
    (comp.lang.c)
  • Re: Does a constraint violation imply undefined behavior?
    ... if there is a constraint violation then all bets ... because it can no longer be interpreted according to the standard. ... diagnostics) means that the implementation has already indicated that ...
    (comp.std.c)
  • Re: Does a constraint violation cause undefined behavior?
    ... If a program contains a constraint violation, ... My own point of view is that because the standard does not say so explicitly, this must be decided on a case by case basis. ... I'd be happy to rely upon market forces, rather than the standard, to force implementors to handle z++ as z=z+1, if they choose to accept the code. ...
    (comp.std.c)
  • Re: Is this an error or undefined behaviour?
    ... a constraint violation, and the compiler is allowed to reject the ... The next example was more like what I expected: 6.5.3.2p1 - if an implementation chooses to translate and execute code which attempts to take the address of a variable declared with the 'register' keyword, I believe it is still bound by 6.5.3.2p1, which would essentially mean that it cannot place such a variable in a register. ... However, if violating a constraint always leaves the behavior undefined behavior, I think it would be clearer to say so explicitly. ...
    (comp.lang.c)
  • Re: +++i
    ... >> I think you mean constraint violation. ... Isn't what the standards term a constraint ... phrase "semantic error", certainly. ...
    (comp.lang.c)