4 ms·
The code copies the origin string up to and including the null terminator, which must exist as strlen was used to obtain the size.
by 0x09 9y ago
The code copies the origin string up to and including the null terminator, which must exist as strlen was used to obtain the size.
- xyzzy4 9y agoIt doesn't copy the null terminator because it's using memcpy. It's only copying the string without the null byte.
- detaro 9y agoI think you are missing the +1 behind the call to strlen. strlen reports the number of chars before the zero-byte, adding one includes it.
- hedora 9y agoI go back and forth between putting the +1 on the first line, or repeating it in the other lines. Putting it on the first line is harder to screw up, but harder to read. For real programs you basically have to put it in the initial computation, or it will be forgotten somewhere later (maybe in a later commit).
- masklinn 9y ago> Putting it on the first line is harder to screw up, but harder to read. Mayhaps "len" should be renamed to something clearer?