Sponsored by Travelzoo
Take Advantage of Ridiculously Low Holiday Airfares view!
travelzoo.com - Flights $52 and up for Thanksgiving, Christmas & New Year. But move on it now.
11 Comments
- newtonapple, on 10/12/2007, -6/+11p9's back? Welcome back.
- 4degrees, on 10/12/2007, -0/+4its always fun to see bonehead pointer mistakes. C++ is a big drawer of sharp knives, if you dont know what you are doing, someone is going to get cut.
- kanhar, on 10/12/2007, -3/+6you guys need to go to www.fogcreek.com and read Joel's notes on software Development. It'll give you more insight on how to prevent errors, especially his 12 point test which underlines each and every important factor in software development.
Kanhar Munshi - panique, on 10/12/2007, -2/+5LOL. What's the schedule for your next tantrum?
- nonsequitor, on 10/12/2007, -0/+1Pointers and casting can be damn useful too. My favorite trick with pointers has got to be for input buffer parsing. Say your trying to get an unsigned int out of 4 bytes...
unsigned int Word;
unsigned char Input[16];
...important stuff...
Word = *(unsigned int*)&input[3];
Lets see you do that in one line of Ada. Of course this amount of power can be misused very easily, if you were to recast a byte pointer to a single byte to an int pointer and dereference it for a write, you just stomped the next 3 bytes after it.
The one reason for bugs in this article I think should embarrass most programmers out there is cut and paste related bugs. I learned early on that if you're cutting and pasting, you're doing something wrong. Better to look at how to refactor the code to generalize the repeated part into some sort of helper function. Repeated code means repeated bugs which will most likely not be fixed concurrently. - inactive, on 10/12/2007, -3/+4FTA: "I can remember the exact instant when I realized [about debugging] that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
I remember that day too: it was dark, oh so very dark. - jnorris441, on 10/12/2007, -0/+1Sentence fragment. Wait, sentence fragment is a sentence fragment....
- tblanchard, on 10/12/2007, -0/+0Some bad advice there.
char *get_name() {
char temp_name = malloc(10);
strncpy(temp_name, "Ben Chelf", 9);
temp_name[9] = 0;
return temp_name;
}
The bug is in the design of the calling interface. He's traded a memory access error for a memory leak. The caller should have to pass in a pointer to the destination.
char *get_name(char* dest, size_t len) {
strncpy(dest, "Ben Chelf", len-1);
dest[len-1] = 0;
return dest;
} - achacha, on 10/12/2007, -3/+3Some of the code listed won't even compile, seeing it automatically tells me it was written either by a junior programmer or a cheap contractor pretending to be a good programmer and in most cases you get what you pay for.
I find it amazing that companies complain about bugs and poorly written code when they are constantly trying to cut corners, hire junior people for complex programming tasks, push artificial deadlines based on quarterly numbers and worst of all let product managers who are often less knowledgeable about the product, dictate to developers what and how they need to do their job. Often, it's a multilayered problem of poor management.
I second the Joel blog entry on this, he makes a lot of good points. - xjqcf, on 10/12/2007, -0/+0You know, I'm not really other than a very casual programmer, let alone a c programmer, but I spotted an unintentional error (meaning, not the error the author intended to illustrate).
One of the code snippets (also repeated in subsequent examples was this:
char *get_name() {
char temp_name = malloc(10);
strncpy(temp_name, "Ben Chelf", 9);
temp_name[9] = 0;
return temp_name;
}
(Damn article had the code snippets as jpgs; hope I didn't add any errors of my own :) )
This code was supposed to be ok, as is, but the presumption was that others relying on it were not informed that the returned pointer was malloc'ed and, thus, needed to be freed when no longer needed. The only problem here is that malloc is returning a pointer to temp_name, which is declared as char.
Now, this article is clearly a spam for a product, or products, of the company he links to. The thrust of these products which the author is trying to sell are static analysis tools. If these tools are so great, and people should shell out money for them, shouldn't this be demonstrated by using them on the code snippets, which should have found not just the errors that were being illustrated but also the unintentional ones? - dwight0, on 10/12/2007, -11/+4welcome back ps9!
hey ps9 welcome back!
wow its ps9! welcome back!


What is Digg?