M
Markus Becker
Netocrat said:This has the overhead of copying (CAPACITY_OF_DST - 1 - strlen(src)) zero
I had expected this and thought of an appropriate answer,
but in the meantime I have learned a few things. Anyway,
the answer to this argument is that otherwise you always
have the overhead of calling strlen(src), which shouldn't
be much different.
And I have slept a good time. Up to my previous posting
I've had about 4 hours of sleep this whole year.
It also silently truncates strings that are oversize: this semantic is not
You can check that with a (strlen(dst)<CAP_OF_DST-1) afterwards.
...which avoids the redundant copying overhead. Often the call to
but does not copy any data. This too might be sub-optimal.
strlen(src) is required for a prior operation and its result is at hand
anyhow. (the comparison shouldn't include an equality test btw)
Right, but I was very tired.
Well, if truncating the string is appropriate semantics, then fine.
I see we agree on the fact that it mostly depends, but there are
several possibilities to handle strcpy et al. without too much
hassle.
Another possibility that may be appropriate is to (re)assign a buffer
large enough to hold the string. At least this code snippet gives you the
opportunity to take that action.
See my answer to targeur fou. Thanks to you, too!
Markus