void* l = dlsym(lib,"_ZN11Environment9LibLogger14log_processingEiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjS6_z");
*(void **)&log_fcn = l;
std::cout<<"Address"<<<<l<<"LOG_FCN "<<log_fcn<<std::endl;
我正在嘗試列印地址,l但log_fcn兩者都不相同。為什么會這樣,我怎樣才能獲得dlsym分配給函式指標的地址?
輸出示例:
l= 0x7efe10eabaa0 LOG_FCN 1
void (*log_fcn)(int level, std::string frmt, unsigned int line_no, std::string file_name, ...); 函式宣告
uj5u.com熱心網友回復:
有一個operator <<for void*,但沒有 for 函式指標。
函式指標被隱式轉換為bool、 not void*,并且默認bool列印為0or 1。
您的1輸出表明這log_fcn不是空指標。
列印時轉換它:
std::cout << "Address" << l << "LOG_FCN "<< reinterpret_cast<void*>(log_fcn) << std::endl;
我建議您也以傳統形式進行分配,方法是鑄造右側:
log_fcn = reinterpret_cast<decltype(log_fcn)>(l);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474914.html
