string - Can I read any readable valid memory location via a (unsigned) char* in C++? -
string - Can I read any readable valid memory location via a (unsigned) char* in C++? -
my search foo seems lacking today.
i know if legal according std c++ inspect "any" memory location via (unsigned(?)) char*. any location mean valid address of object or array (or within array) within program.
by way of example:
void passanyobjectorarrayorsomethingelsevalid(void* pobj) { unsigned char* pmemory = static_cast<unsigned char*>(pobj) mytypeidentifyier x = trytofigureoutwhatthisis(pmemory); }
disclaimer: question purely academical. not intend set production code! legal mean if it's legal according standard, if work on 100% of implementations. (not on x86 or mutual hardware.)
sub-question: static_cast
right tool void* address char* pointer?
c++ assumes strict aliasing, means 2 pointers of fundamentally different type not alias same value.
however, correctly pointed out bdonlan, standard makes exception char
, unsigned char
pointers.
thus, in general undefined behaviour pointer type read any deliberate address (which might type), particular case of unsigned char
in question allowed (iso 14882:2003 3.10(15)).
static_cast
compile-time type checking, unlikely work. in such case, want reinterpret_cast
.
c++ string pointers void-pointers memory-access
Comments
Post a Comment