3 ms·
If this is the case, why aren’t the stdlib functions defined this way? In all of the history of the longest-lived production language family besides FORTRAN, th
by b33j0r 5y ago
If this is the case, why aren’t the stdlib functions defined this way? In all of the history of the longest-lived production language family besides FORTRAN, this blog post is the first voice to point out that memcpy should be the same operation as strcpy for null-terminated strings? What is going on here?
(Rust crowd snickers as they unwrap<‘jk> &mut *foo_buf)
- gompertz 5y agoCurious as well, but when I look at the glibc source for strncpy it is calling memcpy....https://code.woboq.org/userspace/glibc/string/strncpy.c.html https://code.woboq.org/userspace/glibc/string/strncpy.c.html It all seems to depend on the compiler vendor.
- comex 5y agoYou could implement strcpy(a, b) as memcpy(a, b, strlen(b) + 1), but it would be slower, since it makes two separate passes over b (one to calculate the size, one to copy). The post seems to be arguing that in most cases where you would call strcpy, you should know the size already, because otherwise you wouldn't know whether the source string was short enough to fit in the destination buffer.