Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Practical advantages of std::copy, std::equal, std::fill over memcpy (and strcpy), memcmp (and strcmp), memset

Many posts compare the performance of these functions (std::copy, std::equal, std::fill), but I haven’t found what justifies the replacement by these new functions over the old one (std::memcpy, std::memcmp, std::memset) from a practical point of view. Of course, the list of functions is not exhaustive.

What are the practical advantages of these new versions ?

If performance is not a criterion for choosing, is it desirable (time permitting) to replace all occurrences of these C-style functions with the C++ versions ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried to understand why some functions have been introduced in c++ and if it is necessary to do the replacement.

>Solution :

memset has two drastic limitations.

First the container. You can only memset more than a single element to a container that uses contiguous memory. It can work for std::array or std::vector but not for most other containers.

Next, the elements. You can only memset trivially copyable objects. Already something as "simple" as std::string is not trivially copyable.

memset can be used in some cases. The other functions you mention are more generally applicable and when they can be replaced by memset the compiler can do that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading