5 ms·
Does C++ code even use memcpy these days? A honest question.
by nine_k 5y ago
Does C++ code even use memcpy these days? A honest question.
- pjmlp 5y agoSurely. It is part of ISO C++, and not everyone agrees with C++ safe coding practices.
- agent327 5y agoIn practice it's much easier to use assignment or copy construction, thus avoiding the dangerous memcpy. Same goes for those memsets and bzero's beloved of C-programmers: just make the class do it for you; it always works, cannot be forgotten, cannot be done wrong, and is just much easier to begin with. Or, if you have a C-struct you need to interface with, just declare it as 'foo myfoo = {0};'. That also zero's out the whole thing, and the compiler won't get the size wrong.
- pjmlp 5y agoI fully agree.
- jeffbee 5y agomemcpy is the only defined way to type pun in C++. Anyone intentionally avoiding it probably has UB instead.
- pjmlp 5y agoStarting with C++20, std::bit_cast is also an option. https://en.cppreference.com/w/cpp/numeric/bit_cast https://en.cppreference.com/w/cpp/numeric/bit_cast