After reading this question: What are the problems of a zero-terminated string that length-prefixed strings overcome? I started to wonder, what exactly is stopping a C implementation from allocating a few extra bytes for any char
or wchar_t
array allocated on the stack or heap and using them as a "string prefix" to store the number N
of its elements?
Then, if the N
-th character is '\0'
, N - 1
would signify the string length.
I believe this could mightily boost performance of functions such as strlen
or strcat
.
This could potentially turn to extra memory consumption if a program uses non-0
-terminated char
arrays extensively, but that could be remedied by a compiler flag turning on or off the regular "count-until-you-reach-'\0'
" routine for the compiled code.
What are possible obstacles for such an implementation? Does the C Standard allow for this? What problems can this technique cause that I haven't accounted for?
And... has this actually ever been done?