P
pemo
I believe that in languages like Pascal, the length of a string is encoded
in some sort of 'header' data structure, and that if a programmer wants to
know the length of such a string, the resultant discovery is therefore very
fast.
In C, functions like strlen() have to traverse the string in order to find
the null terminator. This requires a comparison of each char in the string
to '\0' of course.
Therefore, string functions (the majority I can think of all require some
'end searching') are necessarily slower than their Pascal counterparts.
Which, seems to go against a bit of C philosophy - speed is everything!
Of course, if a C implementation chose to perhaps keep track of, and store a
C string's length at perhaps a negative offset, e.g. strlen of s =
((unsigned long int *)s)[-1]); this would result if faster string
operations.
However, this would impose a limit on a string's length - but, surely, a
practical one!?
So, to the questions!
Are my assertions correct?
Would such an implementation break the C standard?
Does anyone know of an implementation that perhaps does something like this?
in some sort of 'header' data structure, and that if a programmer wants to
know the length of such a string, the resultant discovery is therefore very
fast.
In C, functions like strlen() have to traverse the string in order to find
the null terminator. This requires a comparison of each char in the string
to '\0' of course.
Therefore, string functions (the majority I can think of all require some
'end searching') are necessarily slower than their Pascal counterparts.
Which, seems to go against a bit of C philosophy - speed is everything!
Of course, if a C implementation chose to perhaps keep track of, and store a
C string's length at perhaps a negative offset, e.g. strlen of s =
((unsigned long int *)s)[-1]); this would result if faster string
operations.
However, this would impose a limit on a string's length - but, surely, a
practical one!?
So, to the questions!
Are my assertions correct?
Would such an implementation break the C standard?
Does anyone know of an implementation that perhaps does something like this?