This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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
  • Now the question is which one of these is a pointer to offset 0 (in a particular memory space) and which is NULL?

    From the point of view of the C standard, I think it's pretty clear: they're all NULL, and should compare equal to any null pointer.

    Based on the definition of NULL (which is defined as ((void *) 0) ) the only candidate for NULL is a generic pointer to idata address 0x00.

    You're thinking of (generic) pointers as integers, it seems. But they aren't numbers. They're abstract thingies that behave somewhat, but not quite like numbers.

    Actually, the C standard does not define NULL as (void *)0, either. #define NULL 0 would be every bit as valid.

    My point would be that in a comparison of the type

    (generic pointer == 0)
    or
    (generic pointer == NULL)

    If pointer is being compared to a null pointer, the compiled code should ignore the "memory class" or "segment" byte.

    If you apply the rules of standard C, and for a moment assume that memory-specific pointers should behave like "pointers" as known by the standard, then the rules about null pointers and null pointer constants strictly require that

    	(void *)(xdata char *)0 == 0

    because ((xdata char *)0) is a valid null pointer by construction, and casting a NULL pointer to another pointer type still gives a null pointer.

Reply
  • Now the question is which one of these is a pointer to offset 0 (in a particular memory space) and which is NULL?

    From the point of view of the C standard, I think it's pretty clear: they're all NULL, and should compare equal to any null pointer.

    Based on the definition of NULL (which is defined as ((void *) 0) ) the only candidate for NULL is a generic pointer to idata address 0x00.

    You're thinking of (generic) pointers as integers, it seems. But they aren't numbers. They're abstract thingies that behave somewhat, but not quite like numbers.

    Actually, the C standard does not define NULL as (void *)0, either. #define NULL 0 would be every bit as valid.

    My point would be that in a comparison of the type

    (generic pointer == 0)
    or
    (generic pointer == NULL)

    If pointer is being compared to a null pointer, the compiled code should ignore the "memory class" or "segment" byte.

    If you apply the rules of standard C, and for a moment assume that memory-specific pointers should behave like "pointers" as known by the standard, then the rules about null pointers and null pointer constants strictly require that

    	(void *)(xdata char *)0 == 0

    because ((xdata char *)0) is a valid null pointer by construction, and casting a NULL pointer to another pointer type still gives a null pointer.

Children
No data