Archived

This topic is now archived and is closed to further replies.

What is a null-terminated string?

This topic is 5743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This may have come up before but I''ve just started doing programming in directx and C++ and I keep hearing the term "null-terminated string". What exactly is the difference between a null-terminated and non-null-terminated string? And when in general we use the term "string", do we mean null-terminated or non-null-terminated? -Allen P

Share this post


Link to post
Share on other sites
a string must always be null terminated or you are gonna ruun into some serious problems.
a string by definition a string of characters ended by a null character.
so if i was to cout a string that wasnt null terminated then it would keep printing out the characters starting at the address where the string starts and continue printing until is reaches a null character.....if it doesnt reach one then it just keeps on going and can run into other memory of your program

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites
A null terminated string can be thought of as an array of char where the last element is equal to ''\0''. You might also encounter a list of such strings, in which case the final element in the list will have two null terminators ''\0\0'' to signal the end of the list. A non null terminated string lacks the ''\0'' at the end. The null terminator reqirement harkens back to the C foundation of the win32 api. One purpose of having a null terminator is to allow for the proper functioning of C library functions like ''strlen'', which use the null terminator in order to break from loops. The null terminator is implied when specifically refering to a C string. In C++ it might mean an instance of the string class or the string class itself.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by ncsu121978
a string must always be null terminated or you are gonna ruun into some serious problems.
a string by definition a string of characters ended by a null character.
so if i was to cout a string that wasnt null terminated then it would keep printing out the characters starting at the address where the string starts and continue printing until is reaches a null character.....if it doesnt reach one then it just keeps on going and can run into other memory of your program

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T


Change that to ''A C string''...

Some other programming languages use length fields instead of termination characters.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Some other programming languages use length fields instead of termination characters.


Most notably Pascal. As a sidenote - Excel uses Pascal strings internally.



The computer was conceived as a tool to reduce complexity. Some people found this loss of complexity unacceptable, and developed UNIX to amend the situation.

Share this post


Link to post
Share on other sites
Here''s a little hard core explanation to that question. If figuring your familiar with assembly programming a little bit.

C/C++ when compiled and linked gets translated into machine code. Now assembly uses interupts to access I/O from a PC. There are tones of available interupts. Now the interupt that C/C++ compiler uses to manage strings (output/input) looks at the data segment where the String pointer is pointing too (since all strings are an array of characters, in essence are pointers to the characters). Now the interupt will keep reading the characters until the null character is reached. Now there are some interupts where you can specify what the terminating character is. For example TASM (Turbo Assembler) uses HEX 24 ($) to terminate the characters.

So as you can imagine C/C++ and other programming languages have different variations for dealing with strings. Like in Java, Java has its own String Class, which has built in functions to automatically determine how long the actual string is without having to manually inserting a terminating character at the end of every string.

So basically what you have to remeber, A string is an array of characters that is stored in memory (Data Segment) now if there is no terminating character at the end of your string and your programming language is looking for one, its not going to stop reading characters until it hits one, and your going to end up with all the crap that is in memory after your string.

If you would like me to elaborate on any of this ICQ me and ill gladley help you understand.

-=Carnage=-
Let the carnage begin...

Share this post


Link to post
Share on other sites
the NULL terminated string, or C string, is simply a standard for c++ libraries, like string, iostream, and etc. Null terminated strings are the most practical, effecient, and dangerous sort of strings.

in C++ when you use the term string then you are most likely (ultimately) refering to a C string. however, you might be refering to a class, like string, which encapsulates the C string.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Carnage_frank
Here''s a little hard core explanation to that question. If figuring your familiar with assembly programming a little bit.

C/C++ when compiled and linked gets translated into machine code. Now assembly uses interupts to access I/O from a PC. There are tones of available interupts. Now the interupt that C/C++ compiler uses to manage strings (output/input) looks at the data segment where the String pointer is pointing too (since all strings are an array of characters, in essence are pointers to the characters). Now the interupt will keep reading the characters until the null character is reached. Now there are some interupts where you can specify what the terminating character is. For example TASM (Turbo Assembler) uses HEX 24 ($) to terminate the characters.

...





What in the world is this all about?


Share this post


Link to post
Share on other sites