Back to Basics!
What is a null-terminated string? What is a Pascal-string? And finally what is a f#@&ed string?
What is the problem with this code?
char bigString[1000];
char *p = bigString;
bigString[0] = ‘\0’;
p = mystrcat(p,”John, “);
p = mystrcat(p,”Paul, “);
p = mystrcat(p,”George, “);
p = mystrcat(p,”Joel “);
…
…
Does malloc do garbage collection? ;-)
Why is it better always to malloc 2^n bytes?
Why “SELECT author FROM books?” is slow when done on xml?
Do you understand what this code does?
void foo( char* a, char* b )
{
while (*a) b++;
while (*a++ = *b++);
}
I know most of our answers will be “Why should I care about all pointer nonsense when I’m coding in my beautiful language like Java or Perl?” This reminds me of an old discussion we had during lunch. Is it necessary for application programmers to know low level stuffs? Joel says “Yes”. Read the following concluding paragraphs from his article:
For those three gracious members of my audience who are still with me at this point, I hope you’ve learned something or rethought something. I hope that thinking about boring first-year computer-science stuff like how strcat and malloc actually work has given you new tools to think about the latest, top level, strategic and architectural decisions that you make in dealing with technologies like XML. For homework, think about why Transmeta chips will always feel sluggish. Or why the original HTML spec for TABLES was so badly designed that large tables on web pages can’t be shown quickly to people with modems. Or about why COM is so dang fast but not when you’re crossing process boundaries. Or about why the NT guys put the display driver into kernelspace instead of userspace.
These are all things that require you to think about bytes, and they affect the big top-level decisions we make in all kinds of architecture and strategy. This is why my view of teaching is that first year CS students need to start at the basics, using C and building their way up from the CPU. I am actually physically disgusted that so many computer science programs think that Java is a good introductory language, because it’s “easy” and you don’t get confused with all that boring string/malloc stuff but you can learn cool OOP stuff which will make your big programs ever so modular. This is a pedagogical disaster waiting to happen. Generations of graduates are descending on us and creating Shlemiel The Painter algorithms right and left and they don’t even realize it, since they fundamentally have no idea that strings are, at a very deep level, difficult, even if you can’t quite see that in your perl script. If you want to teach somebody something well, you have to start at the very lowest level. It’s like Karate Kid. Wax On, Wax Off. Wax On, Wax Off. Do that for three weeks. Then Knocking The Other Kid’s Head off is easy.
Go here for the original article: http://www.joelonsoftware.com/articles/fog0000000319.html
:-)