c++chexsize-t

Generating hex value depending on size of size_t


I'm trying to generate a value denoting an invalid address. On 32-bit platforms, I want the value 0xaaaaaaaa. On 64-bit platforms, I want 0xaaaaaaaaaaaaaaaa. Is there a way to do this without using preprocessor directives?


Solution

  • For the two's complement representation of integers you can use memset declared in header <string.h> in C or std::memset declared in header <cstring> in C++ as for example

    size_t value;
    memset( &value, 0xaa, sizeof( size_t ) );
    

    Or in the both languages you can write

    size_t value = ( size_t )-1 / 3 * 2;