Olaf Schlüter
1 min readDec 28, 2023

The funny thing is, that rust does compile to the same efficient code like C++ does with __restrict without the need to resort to unsafe operations (of course, not with your rust code, but with the use of iterators). This is the benefit of not being able to have overlapping areas of memory to copy. You can still do that, if you need to for programming very close to the metal and then you need to do unsafe operations - which in this case will be slower then the safe ones as they would be like what C++ produces without restrict.

And looking at that C++ (really C code) in this section explains why the NSA discourages the use of C (and C++) as a programming language. All is wrong with the code: count isn't related at all to the size of either src nor dst, no pre-condition making this code safe is garantueed. The bare minimum für the mul_by_2 function in either variant would need to be to restrict the number of copy iterations to the minimum of either memory size and count. But as C pointers do not carry around with them the size of the memory they are pointing at, one isn't even able to calculate that minimum.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Olaf Schlüter
Olaf Schlüter

Written by Olaf Schlüter

IT security specialist, Physicist by education, believing in God as for the exceptional harmony of the laws of nature to create and support life.

Responses (1)

Write a response