Type promotion bug?

Specific pointer -> generic pointer -> boolean (i.e. implicit compare to 0) seems not to work as expected.

void main()
{
  char* p;
  char* q;
  char xdata* xp = NULL;

  bit fail = FALSE;

  // This fails.
  if ( p = xp )
  {
    fail = TRUE;
  }

  // This is ok.
  if ( ( q = xp ) == NULL )
  {
    fail = TRUE;
  }
}

Parents
  • A pointer to xdata location 0 isn't 0. It's 1'0000H. If the result of the assignment converts to integer, then it would be a non-zero value. I agree that such might not be what you would expect, undesirable even unto bugginess.

    It would perhaps be better if tagged pointers ignored the tag byte when converting to ints. But then you'd have a harder time dealing with far addresses. And a context-sensitive conversion would be confusing as well.

    How do you have NULL #defined? "((void*)0)"? Presumably that brings the type of the second expression back to pointer, including the tag, so the tag byte gets handled properly. (And would this definition be memory-model dependent, as there has to be a default location for that "void"?)

Reply
  • A pointer to xdata location 0 isn't 0. It's 1'0000H. If the result of the assignment converts to integer, then it would be a non-zero value. I agree that such might not be what you would expect, undesirable even unto bugginess.

    It would perhaps be better if tagged pointers ignored the tag byte when converting to ints. But then you'd have a harder time dealing with far addresses. And a context-sensitive conversion would be confusing as well.

    How do you have NULL #defined? "((void*)0)"? Presumably that brings the type of the second expression back to pointer, including the tag, so the tag byte gets handled properly. (And would this definition be memory-model dependent, as there has to be a default location for that "void"?)

Children
More questions in this forum