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
  • This will strip out the memory types on both sides and then a valid comparison can be made. What do you think?

    I'd probably do something like this:

    #define ISNULL(x)  (((unsigned) (void *) (x)) == 0)
    
    void * p;  // gerneric pointer
    .
    .
    .
    if (ISNULL(p))  {
      // NULL pointer detected
      }

    This has the effect of removing the memory selector and it's really obvious what you are testing.

    Jon

Reply
  • This will strip out the memory types on both sides and then a valid comparison can be made. What do you think?

    I'd probably do something like this:

    #define ISNULL(x)  (((unsigned) (void *) (x)) == 0)
    
    void * p;  // gerneric pointer
    .
    .
    .
    if (ISNULL(p))  {
      // NULL pointer detected
      }

    This has the effect of removing the memory selector and it's really obvious what you are testing.

    Jon

Children
No data