User Tools

Site Tools


c

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
c [2015/12/31 02:27] – [Use strncpy() rather than strcpy() if at all possible] adminc [2020/10/17 03:05] (current) – ↷ Links adapted because of a move operation 114.119.151.1
Line 1: Line 1:
-=====Writing C=====+====== Decent C======
 I've been writing c for over 30 years, but it can still be a nightmare - the problems are well documented elsewhere. The discipline of writing device drivers for Linux/Solaris/HP-UX/AIX etc etc and crypto code for RSA (thanks, Michael for the excellent code-reviews - I learned so much) have led to a few points that help make code clearer, easier to maintain and less likely to contain memory leaks and other faults. I've been writing c for over 30 years, but it can still be a nightmare - the problems are well documented elsewhere. The discipline of writing device drivers for Linux/Solaris/HP-UX/AIX etc etc and crypto code for RSA (thanks, Michael for the excellent code-reviews - I learned so much) have led to a few points that help make code clearer, easier to maintain and less likely to contain memory leaks and other faults.
 ====Single return point==== ====Single return point====
-Every routine should have a single return point. All possible execution paths go through this point and therefore have the chance to have resources released. The main thing, of course, is to free resources that are no longer needed eg memory that has been malloc()'ed, file/socket descriptors and in device drivers, to release DMA and interrupt resources.+Every routine should have a single return point. All possible execution paths go through this point and therefore have the chance to have resources released. The main thing, of course, is to free resources that are no longer needed eg memory that has been malloc()'ed, file/socket descriptors and in device drivers, to release DMA and interrupt resources. See the following example.
 ====Error responses should be close to the failing statement==== ====Error responses should be close to the failing statement====
 and not dangling at the end of some huge }}}} block ten pages away!!! Deal with it _now_, to hell with making the Happy Path clearer - the Happy Path is easy, it's the errors that make the thing beastly. and not dangling at the end of some huge }}}} block ten pages away!!! Deal with it _now_, to hell with making the Happy Path clearer - the Happy Path is easy, it's the errors that make the thing beastly.
Line 12: Line 12:
 Do it even when it looks stupidly simple - later, when the routine is made more elaborate, then the logic is in place and you will remember to cleanup: Do it even when it looks stupidly simple - later, when the routine is made more elaborate, then the logic is in place and you will remember to cleanup:
  
-<code>+<code c>
 /* @return 0 if file is foobarable. Otherwise 1.  /* @return 0 if file is foobarable. Otherwise 1. 
  */  */
Line 75: Line 75:
 ====Return an error code==== ====Return an error code====
 If at all possible, always return an int to indicate success or failure eg. 0 for success, anything else for an error. Or return a pointer on success and NULL on failure. If at all possible, always return an int to indicate success or failure eg. 0 for success, anything else for an error. Or return a pointer on success and NULL on failure.
-====Document all parameters and return values+====Document all parameters and return values====
 Just do it. No, the source code is not enough documentation. And keep it up to date as you code. You might as well use doxygen format in case the employer cultivates a taste for it. Just do it. No, the source code is not enough documentation. And keep it up to date as you code. You might as well use doxygen format in case the employer cultivates a taste for it.
 ====exit values etc==== ====exit values etc====
 Unless you've got a really good excuse, the program should exit(0) on success and non-zero on failure (note that exit code can only be up to 8-bits long). Unless you've got a really good excuse, the program should exit(0) on success and non-zero on failure (note that exit code can only be up to 8-bits long).
-As with [[DecentBash]], the program should send error messages (prefixed with the program name) to stderr, output to stdout. It should recognise -h (no matter what) and possibly --help and print help to stdout (and return 0!). -v and --verbose are also pretty standard.+As with [[unixscripts:3-decentbash]], the program should send error messages (prefixed with the program name) to stderr, output to stdout. It should recognise -h (no matter what) and possibly --help and print help to stdout (and return 0!). -v and --verbose are also pretty standard.
  
 In fact, why not use argp(3) to make option processing easy? In fact, why not use argp(3) to make option processing easy?
Line 85: Line 85:
 Functions must fit on a single editor page. OK I have a hi-res screen and get 70 lines on it, but that's OK. I also stick to K&R code layout (as above) so more context fits on a single screen. Functions must fit on a single editor page. OK I have a hi-res screen and get 70 lines on it, but that's OK. I also stick to K&R code layout (as above) so more context fits on a single screen.
 ====valgrind==== ====valgrind====
-You're not done until valgrind says you're done!!+You're not done until valgrind says you're done and your program leaks no memory!!
 ====Keep it simple==== ====Keep it simple====
-As Kernighan'is reputed to have said, debugging (and tuning) is twice as hard as coding so if you code to the limit of your ability, then you will not be able to debug or tune it! This sin is most often commited by new programmers eager to show how clever they are. I'm not usually very impressed.+As Kernighan is reputed to have said, debugging (and tuning) is twice as hard as coding so if you code to the limit of your ability, then by definition, you will not be able to debug or tune it! This sin is most often commited by new programmers eager to show how clever they are. I'm not usually very impressed.
 ====Have it reviewed==== ====Have it reviewed====
-I've learned so much from having others (contructively) critique my code.+I've learned so much from having others (constructively) critique my code.
 ====Links==== ====Links====
 Here's someone who thinks the same way: Here's someone who thinks the same way:
c.1451554059.txt.gz · Last modified: 2015/12/31 02:27 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki