Why member function address are so far away from free functions?

社会主义新天地 提交于 2020-02-02 13:07:31

问题


Taking this example: https://godbolt.org/z/gHqCSA

#include<iostream>

template<typename Return, typename... Args>
std::ostream& operator <<(std::ostream& os, Return(*p)(Args...) ) {
    return os << (void*)p;
}

template <typename ClassType, typename Return, typename... Args>
std::ostream& operator <<(std::ostream& os, Return (ClassType::*p)(Args...) )
{
    unsigned char* internal_representation = reinterpret_cast<unsigned char*>(&p);
    os << "0x" << std::hex;

    for(int i = 0; i < sizeof p; i++) {
        os << (int)internal_representation[i];
    }

    return os;
}
struct test_debugger { void var() {} };
void fun_void_void(){};
void fun_void_double(double d){};
double fun_double_double(double d){return d;}

int main() {
    std::cout << "0. " << &test_debugger::var << std::endl;
    std::cout << "1. " << fun_void_void << std::endl;
    std::cout << "2. " << fun_void_double << std::endl;
    std::cout << "3. " << fun_double_double << std::endl;
}

// Prints:
//    0. 0x7018400100000000000
//    1. 0x100401080
//    2. 0x100401087
//    3. 0x100401093

I see the address of the member function is 0x7018400100000000000, which is understandable because member functions pointers have 16 bytes while free function as 0x100401080 have only 8 bytes.

However, why the member function address 0x7018400100000000000 is so far away from free function address 0x100401080? i.e., |0x7018400100000000000 - 0x100401080| = 0x70184000FFEFFBFEF80?

Why it is not closer i.e., something like 0x100401... instead of 0x701840...? Or I am printing the member function address wrong?


回答1:


Your architecture is little-endian. The low byte of the address is in the first byte of p, so your address is being printed out backwards.




回答2:


Fixed code which automatically detects little/big endian: https://godbolt.org/z/XSvT5R

#include <iostream>
#include <iomanip>
#include <sstream>

inline bool is_big_endian() {
    long int longvalue = 1;

    // https://stackoverflow.com/questions/8978935/detecting-endianness
    unsigned char* representation = reinterpret_cast<unsigned char*>(&longvalue);
    return ( (unsigned) representation[sizeof(long int) - 1] ) == 1;
}

template<typename Pointer>
std::ostream& print_pointer(std::ostream& os, const Pointer& pointer) {
    const unsigned char* representation = reinterpret_cast<const unsigned char*>(&pointer);

    int precision = 0;
    bool haszeros = false;

    unsigned firsthexdigit;
    unsigned secondhexdigit;

    std::ostringstream stream;
    stream.flags( os.flags() );
    stream << std::hex;
    #define print_pointer_HEX_DIGIT \
        firsthexdigit = (unsigned) representation[index] >> 4 & 0xf; \
        secondhexdigit = (unsigned) representation[index] & 0xf; \
        if( haszeros || firsthexdigit ) { \
            precision++; \
            haszeros = true ; \
            stream << firsthexdigit; \
        } \
        if( haszeros || secondhexdigit ) { \
            precision++; \
            haszeros = true ; \
            stream << secondhexdigit; \
        }

    if( is_big_endian() ) {
        for(unsigned int index = 0; index < sizeof pointer; index++) {
            print_pointer_HEX_DIGIT
        }
    }
    else {
        for(unsigned int index = sizeof pointer - 1; index >= 0 ; index--) {
            print_pointer_HEX_DIGIT
        }
    }

    if( os.precision() - ++precision > 0 ) {
        return os << "0x" + std::string( os.precision() - ++precision, '0' ) + stream.str();
    }
    return os << "0x" + stream.str();
}

template<typename Return, typename... Args>
std::ostream& operator <<(std::ostream& os, Return(*pointer)(Args...) ) {
    return print_pointer(os , pointer);
}

template <typename ClassType, typename Return, typename... Args>
std::ostream& operator <<(std::ostream& os, Return (ClassType::*pointer)(Args...) ) {
    return print_pointer(os , pointer);
}

struct test_debugger { void var() {} };
void fun_void_void(){};
void fun_void_double(double d){};
double fun_double_double(double d){return d;}

int main() {
    std::cout << "0. " << &test_debugger::var << std::endl;
    std::cout << "1. " << fun_void_void << std::endl;
    std::cout << "2. " << fun_void_double << std::endl;
    std::cout << "3. " << fun_double_double << std::endl;
    std::cout << "4. " << std::setfill('0') << std::setw(16) << fun_void_void << std::endl;
    std::cout << "5. " << std::setprecision(16) << fun_void_double << std::endl;
}
// Prints:
//    0. 0x100402e80
//    1. 0x100401118
//    2. 0x10040111f
//    3. 0x10040112b
//    4. 000000x100401118
//    5. 0x0000010040111f


来源:https://stackoverflow.com/questions/59779964/why-member-function-address-are-so-far-away-from-free-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!