5 ms·
The built-in "strcpy" function has the exact same limitation. Does the author have a problem with it as well? Yes. From the linked chapter: we avoided classic
by mechanical_fish 15y ago
The built-in "strcpy" function has the exact same limitation. Does the author have a problem with it as well?
Yes. From the linked chapter:
we avoided classic style C strings in this book
From an earlier chapter on strings:
The source of almost all bugs in C come from forgetting to have enough space, or forgetting to put a '\0' at the end of a string. In fact it's so common and hard to get right that the majority of good C code just doesn't use C style strings. In later exercises we'll actually learn how to avoid C strings completely.
This is the author's opinion, of course – it's from a book, that should go without saying – but it's not as if the idea of avoiding C strings in general, and "strcpy" in particular, is an oddball or unique point of view. See e.g.:
http://stackoverflow.com/questions/610238/c-strcpy-evil http://stackoverflow.com/questions/610238/c-strcpy-evil
- peapicker 15y ago"the majority of good C code just doesn't use C style strings..." Nice. In the 23 years I have worked on C language products, I've never worked on "good C code" by this definition. The cool thing about this guys book, I guess, is that by avoiding all the things about the language he doesn't like, any reader will be wholly unprepared for C in the Real World after this book.
- listic 15y agoThe approach looks similar to the one in "JavaScript: the good parts" I am worried about the same while reading it: ok, here's the right way to do e.g. inheritance, but how do they do it in real world?
- mechanical_fish 15y agoThis chapter is explicitly teaching people what the author's idea of bad C code is: It has them read some, then tells them specifically what the gotchas are, then asks them to code up test cases for the flaws and run them through Valgrind. Where's the "avoiding" here? And which of the skills being exercised - imagining what kinds of bad things could happen, writing executable test cases, detecting segfaults - are not useful in the real world?
- marshray 15y agoHow about being able to work on a team of other programmers without insisting that everything ought to be done you own, completely unique, way? If he truly had a better, more correct, way to write C code I would join him in trying to change the world. But the first example he gives is just factually wrong.
- tedunangst 15y ago"we avoided classic style C strings in this book" There's the avoiding. The skills being exercised are not a problem, it's the skill not being exercised that is a problem. You cannot be a competent C programmer without understanding strings and their relevant library functions.
- zedshaw 15y agoThat's basically the point of this chapter. It's getting people to think like a hacker and try to break the code in unintended ways. That makes them better programmers and helps when avoiding common mistakes in C code. Using K&R to do this is to give people a set of known good C code samples and show how even those can be broken and misused.
- ori_b 15y agoNoble goal, but the way the chapter is written doesn't really come across that way, in my opinion.
- aptwebapps 15y agoSpeaking as someone who doesn't know C, hasn't read K&R, but who (usually!) has decent reading comprehension, it _does_ come across this way. He's completely explicit about what he's doing and why. I don't understand how people can read that and still feel that he's being unfair. Maybe I'm just too literal-minded.
- ori_b 15y agoCalling string manipulation functions buggy and broken because they fail when you don't pass them a string is both wrong and silly.
- zedshaw 15y agoYes, they'll be pretty good at C unlike you because they'll read the whole book instead of just this one half-finished chapter.
- peapicker 15y ago"they'll be pretty good at C unlike you" Thanks for the code review of my last 23 years of work on C projects. Nice ego that your book must be the only way to learn this. I mostly disagree with your statement about "most good C code" not using stdlib strings/apis. To be fair, from your comments here, it looks like you aren't really saying that -- you are saying we have to be careful when using them. This I know, and carefully guard against, and I do try to break my code regularly. But to assert that "most good C code" doesn't use them fails to meet with my experience. That said, _all_ bad C code (ab)uses them, probably due to carelessness and/or misunderstanding of how things work in C.
- perfunctory 15y ago> unprepared for C in the Real World Oh please. Most C in the "Real World" is utter crap. It doesn't mean there is no place for books teaching "alternative" approaches.
- shabble 15y agoI didn't see it yet from a quick skim of the available chapters, but I'm going to guess that his preferred alternative is 'bstr - The Better String Library' - http://bstring.sourceforge.net/ http://bstring.sourceforge.net/ I've used it the past, and I try to include it in any project that isn't immediately trivial, although the to/from integration with just about all library code ever is a bit of a hassle.
- tptacek 15y agoIt's not just that the to/from is a hassle, it's also that you have to deeply understand the gotchas of ASCIIZ strings to safely do that bridging at all --- so pretending it doesn't exist is probably not a good teaching strategy. (I have no opinion about the article and am only commenting on this thread because I like C).
- zedshaw 15y agoI do not pretend anything about null-terminated strings in the book. In fact, I have many exercises and assignments where they use Valgrind to buffer overflow C strings repeatedly and don't introduce bstring until much later.
- tptacek 15y agoSorry, I haven't read your book, meant to be clear that I was talking in the abstract, but I can see after rereading I wasn't clear enough.
- zedshaw 15y agoI use bstring but there's a few others. Generally it's the easiest way to avoid some common buffer overflows and other problems.