Home > Enterprise >  return address of dlsym and Address of Function Pointer assigned
return address of dlsym and Address of Function Pointer assigned

Time:05-10

void* l = dlsym(lib,"_ZN11Environment9LibLogger14log_processingEiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjS6_z");
*(void **)&log_fcn   = l;
std::cout<<"Address"<<<<l<<"LOG_FCN "<<log_fcn<<std::endl;

I am trying to print address of l and log_fcn but both are not same. Why is that, and how can I get address of dlsym assign to function pointer?

Output example:

l= 0x7efe10eabaa0 LOG_FCN 1

void (*log_fcn)(int level, std::string frmt, unsigned int line_no, std::string file_name, ...); function decleration

CodePudding user response:

There is an operator << for void*, but not for function pointers.
Function pointers are implicitly converted to bool, not void*, and bool prints as 0 or 1 by default.

Your 1 output indicates that log_fcn is not the null pointer.

Convert it when printing:

std::cout << "Address" << l << "LOG_FCN "<< reinterpret_cast<void*>(log_fcn) << std::endl;

I would recommend that you also do the assignment in the conventional form, by casting the right-hand side:

log_fcn = reinterpret_cast<decltype(log_fcn)>(l);
  • Related