Boost logo

Boost :

From: Dan Gohman (dgohman_at_[hidden])
Date: 2002-08-07 12:53:30


As a solution to asserts on pointers with 64-bit platforms, I've used
a function like the following:

 bool is_dereferenceable_pointer(void const* p)
 {
#if some_specific_platform
    unsigned long v = reinterpret_cast<unsigned long>(p);
    return
       v != -1ul // -1 is sometimes used as a special non-dereferenceable
                   // pointer value, for example in the return value of mmap
       &&
       v >= 0x1000 // Many platforms define specific address ranges
                   // for valid pointers.
       ;
#else
    // For platforms without a custom check defined, we
    // just check for NULL.
    return p != 0;
#endif
 }

 assert(is_dereferenceable_pointer(p)); // for some pointer p

In addition to avoiding warnings with 64-bit pointers, it is
more readable IMHO than `assert(p != 0)' and it can be more
aggressive on platforms with documented address spaces.

On MIPS/IRIX, for example, pointer values start at 0x400000 and
go to 0x80000000 in 32-bit ABIs and 0x10000000000 in 64-bit ABIs.

Dan

-- 
Dan Gohman
dgohman_at_[hidden]

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk