On Tue Nov 22, 2005 at 17:01:59 +1100, Jamie Wilkinson wrote: >This one time, at band camp, O Plameras wrote: >>Benno wrote: >>> >> >>Your are wrong. The string is "some words\0". there is no need for extra >>char. > >How do you think strlen() works? > >Also, it's pretty clear you were writing this anyway, but typoed the >placement of the closing parenthesis. > >>Your are wrong. The string is "some words\0". there is no need for extra >>char. > >Your jedi mind tricks do not work on me! > >>>Moral of the story is don't code in C ;) Or if you do use as many tools >>>to help you as possible. -Wall, and a C linter are a good start. >>>Valgrind is also meant to be good, although I haven't >>>used it. >>> >>> >> >>I don't understand this. Please explain. > >Benno's talking about the buffer overruns caused by the incorrect pointer >arithmetic in your examples. You can track them down with "as many tools to >help you as possible", e.g. gcc -Wall, valgrind. > >Or use pascal where the length of a string isn't determined by the position >of the null :)
So yeah, I was reading about pascal strings the other day. Pretty cool. Apparently Mac9 used them in its libraries. (Could easily be wrong there.) The idea is that the first byte of memory holds the length of the string. (Which limits you to a 256 length string.) But means operations strlen is O(1), not O(n). And something like strdup can avoid a double iteration through the list, and strcat can avoid trawling the first list. Etc, etc. If course declaring a string inline becomes a bit tricker: char *s = "\005pants"; And also standard C compiler will waste a byte NULL-terminating it even though you don't need to. So there is your trivia for the day. Benno -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
