Hacker News new | past | comments | ask | show | jobs | submit login

the point of the exercise is to understand how a copy works, and stylistic issues aside, the point is made.

if you supply a function with inputs outside of it's specification (NULL-terminated strings), then undefined behaviour is (by definition) going to happen.

besides, what's to stop someone from calling safercopy like so;

    safercopy(strlen(str1), str1, strlen(str1), str2);
then strlen will fail (albeit a bit more safely - perhaps).

it's a safe bet, that in production code, we'll not be working with fixed length strings. so we need to get the length of the string somehow. all his safercopy does identify a problem that he himself already points out is impossible to solve - how do we differentiate from a NULL-terminated string, and one that isn't?

the only real solution (i can think of) is a string class, where the constructor is guaranteed to return valid (or no) strings. then (assuming other functions can't overwrite our memory - already an unsafe assumption) we could guarantee a safe string copy.

programming is hard.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact