Re: Zero termination

From: Michael D'Errico (mike-list@pobox.com)
Date: Sat Jun 27 2009 - 15:16:41 CDT

  • Next message: Venugopalan G: "Re: Zero termination"

    > Phew!.... am gonna use NULL terminator... XD

    Even if you never encounter U+0000 (NULL) in your text
    processing, using a NULL terminator for a string is
    horribly inefficient. It is much, much better to keep
    track of string length in a separate field. Consider
    the common operation of concatenating two strings. If
    you use NULL terminators, you need to scan the first
    string for the NULL terminator, keeping track of the
    length as you go, then repeat the process for the 2nd
    string, allocate enough memory to hold the combined
    length, and finally copy the memory. If you had the
    lengths available, you could skip the scanning of both
    strings and simply add the two numbers to determine
    how much memory to allocate.

    Of course you can do what you want, but if you look
    around at all the modern object-oriented languages,
    they do not use a NULL terminator for their strings;
    they keep a character count in each string object.

    Mike



    This archive was generated by hypermail 2.1.5 : Sat Jun 27 2009 - 15:21:12 CDT