Thread: Non null-terminated strings

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Non null-terminated strings

    I'm used to strings being terminated with a NULL (or NUL - take your pick) character. I've been told that the <type> string defined in string.h is not NULL terminated. How is string length determined without a NULL character? Is a <type> int prefix attached to the string containing a value of the string length? Pascal uses that structure with a <type> char prefix (hence, Pascal strings can only be 255 character long).

    Thank you.

    Casey

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    However the library implementors want to do it. My understanding is that a length member variable is commonly used.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Casey G View Post
    I'm used to strings being terminated with a NULL (or NUL - take your pick) character. I've been told that the <type> string defined in string.h is not NULL terminated. How is string length determined without a NULL character? Is a <type> int prefix attached to the string containing a value of the string length? Pascal uses that structure with a <type> char prefix (hence, Pascal strings can only be 255 character long).

    Thank you.

    Casey
    Typically it's done with a struct...
    Code:
    typedef struct tString
      { char *string;
         int size;           // used for realloc()
         int length;        // MyStr.len
         int refcount; }  // used by garbage collector
       String;
    As pointed out it is implementation specific but that's the general idea.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2
    Thanks - I'm getting the general drift. I'm new to C++ (No! Really?) and when I was told strings were not null terminated, I was having trouble wrapping my head around the concept. I featured there must be some kind of typedef struct invloved where there is a variable defining the string length, a value for allocating memory for the string, and a pointer to the string. When I was told that a <type> string is not null terminated, I ask how the elements in an array of <type> string are defined (what seperates the array elements?). The person shook his head and said, "I dunno." The blind leading the blind. LMAO.

    I'd like to see the _CDecl for <type> string, but I couldn't find it in string.h. Is it defined in a std???.h file?
    Last edited by Casey G; 05-10-2011 at 06:31 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Casey G View Post
    Thanks - I'm getting the general drift. I'm new to C++ (No! Really?) and when I was told strings were not null terminated, I was having trouble wrapping my head around the concept. I featured there must be some kind of typedef struct invloved where there is a variable defining the string length, a value for allocating memory for the string, and a pointer to the string. When I was told that a <type> string is not null terminated, I ask how the elements in an array of <type> string are defined (what seperates the array elements?). The person shook his head and said, "I dunno." The blind leading the blind. LMAO.
    I'd like to see the _CDecl for <type> string, but I couldn't find it in string.h. Is it defined in a std???.h file?
    _cdecl is a calling convention... I'm guessing you mean the declaration...

    It's likely in the string library and thanks to C++ code hiding, we probably have no access to it. Really... I'm not 100% sure that's how it works but that's how most compilers do it. It is, afterall, implementation defined, which means different libraries can do it in different ways so long as it's syntactically transparent to us as users. GCC might use a totally different approach than VC++ or MinGW... but it really doesn't matter, so long as it works.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Casey G
    I've been told that the <type> string defined in string.h is not NULL terminated.
    Sorry, but what is a "<type> string"? Are you talking about std::string, or more generally about std::basic_string? Furthermore, when you say string.h, do you mean the <string.h> header inherited from C, or the <cstring> C++ version thereof, or the C++ specific <string> header?

    In the case of std::basic_string, or more specifically std::string, the obvious way to implement is to keep track of size and capacity explicitly, rather than keep track of size through a null terminator. In fact, it is possible to embed null characters in a std::string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning a non null-terminated string
    By BattlePanic in forum C Programming
    Replies: 7
    Last Post: 05-04-2008, 10:02 PM
  2. Is argv[] terminated by a NULL string?
    By dwks in forum C Programming
    Replies: 9
    Last Post: 07-24-2005, 10:24 AM
  3. null terminated
    By TeQno in forum C Programming
    Replies: 5
    Last Post: 06-06-2003, 06:10 PM
  4. Null Terminated Arrays
    By sean in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 06-17-2002, 11:39 AM