pointers - C++ Convert Address of Memory To Value? -
in c++, using iostream, can print variable's memory address. example:
std::cout << &variable << std::endl; // illustration result: 002afd84
however, if wanted store memory address variable? such converting memory address string or double(or int, etc.)? or convert string or double(or int, etc.) again, memory address?
i'd various reasons, 1 being: allow me homecoming memory address info within dll programme calling dll , it's functions. on top of this, won't have maintain track of info within dll, since info referenced it's memory address.
i cannot utilize pointers in particular situation due constraints. constraints being: interpreted programming language using not have access pointers. due this, pointers cannot used reference info outside of dll.
as side question, number format memory addresses use? seem seems 8 characters in length, can't figure out format is.
if absolutely need suggest beingness selective types convert to. arbitrarily converting pointers non-pointer types can problematic , introduce problems hard detect. true if using reinterpret_cast
perform conversions. 1 of more mutual issues size of destination type between various platforms. when utilize reinterpret_cast
typically don't warnings loss of precision during conversion.
for situations require convert pointer integral type suggest wrapping these conversion in function template. allow bit of flexibility in performing conversion , can perform compile-time size checks ensure destination type big plenty hold pointer.
something code below might helpful.
template<class desttype, class sourcetype> desttype bubblicious_value_cast(const sourcetype& src) { static_assert(sizeof(desttype) >= sizeof(sourcetype), "destination size small"); homecoming reinterpret_cast<desttype>(src); } int main() { void* ptr = nullptr; int val = bubblicious_value_cast<int>(ptr); }
c++ pointers memory
No comments:
Post a Comment