In what cases should I use memcpy over standard operators in C++?

前端 未结 7 1663
梦毁少年i
梦毁少年i 2020-12-13 06:36

When can I get better performance using memcpy or how do I benefit from using it? For example:

float a[3]; float b[3];

is cod

7条回答
  •  旧时难觅i
    2020-12-13 07:03

    The benefits of memcpy? Probably readability. Otherwise, you would have to either do a number of assignments or have a for loop for copying, neither of which are as simple and clear as just doing memcpy (of course, as long as your types are simple and don't require construction/destruction).

    Also, memcpy is generally relatively optimized for specific platforms, to the point that it won't be all that much slower than simple assignment, and may even be faster.

提交回复
热议问题