Can anyone tell me why the n string functions are seemingly so poorly designed?
strncpy, wcsncpy.
These do not guarantee a terminating null if the source string is longer than the destination, and if the destination is longer than the source they will pad with null bytes.
Who wants this behaviour? The first is dangerous and leads to extra code and the second is totally inefficient. Both are not generally needed.
For example, if I have a function that takes a string and I want to make a copy to modify I can do this:
So to perform this common simple operation with the stdCode:char szCopy[512]; if (strlen(szInput) >= 512) return E_INVALIDARG; strcpy(szCopy, szInput); OR: strncpy(szCopy, szInput, 512); if (szCopy[511] != '\0') return E_INVALIDARG;
functions is quite inefficient.
I have to turn to the MS string library or write my own function to perform this extremely simple operation efficiently:
Am I missing something?Code:if (FAILED(StringCchCopy(szCopy, 512, szInput))) return E_INVALIDARG;



LinkBack URL
About LinkBacks


