I have a std::vector<uint8_t> buffer; I want to load a datatype which is larger than uint8_t (in this example uint16_t target, but could be anything). I know that the content of this object has been copied into position size_t ID in buffer.
As far as I can tell, it is possible to copy the target with:
uint16_t target = *((uint16_t*) &buffer[ID]);
It works with gcc version 9.3.0 on Ubuntu using C++17.
But it certainly looks unintended. So is this safe to use? That is, as long as I know for certain that ID+sizeof(target) is within the buffer, can I be sure that this will compile and run without crashing on all OS?