61_pdfsam_cs2022_lectures

61_pdfsam_cs2022_lectures - (usually Avoid C’s more terse...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Be consistent Choose style conventions and stick with them How are variables and functions named? local variables are one word in lower case, e.g. counter function names are lower case with underscores, e.g. get_name() constants are in upper case, e.g. MAX_SIZE
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Be consistent How are errors reported?, e.g. 1 on success, 0 on failure In what order are function parameters?, e.g. Destination pointer first, then source pointer(s) How is memory management handled?, e.g. Any pointers returned by a function must be free()’d by the caller
Background image of page 2
Always check for errors There is not an exception mechanism in C So you need explicit error checking Investing in error checking saves time later FILE *fp; char *buf fp = fopen(“C:\\data\\datafile.txt”, “r”); // print error msg if file not opened properly if(!fp) { perror(“error”); } buf = (char *) malloc( 25000000 ); assert(buf != NULL); // ensure memory // allocated successfully
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Write clear code Choose simpler coding strategy Even at the expense of speed or compactness
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (usually) Avoid C’s more terse and confusing features Use braces, parentheses and spaces Avoid using ++ or -- in complex expressions If statements usually clearer than conditionals a=(b++>7?b:a>1?1:a) if(b > 7) a = b; else if(a > 1) a = 1; else a = a + 3; b++; How not to write C code… C code can become incredibly complicated Inspired the International Obfuscated C Contest (IOCC) main(O){int I,Q,l=O;if(I=l*4){l=6;if(l>5)l+=Q-8?l-(Q=getchar()-2)%2:l;if(Q*=2)O+="has dirtiest IF"[(I/-Q&12)-l/Q%4];}return 8+O%4;} Optimizing When speed really matters, use C Can be 10-100x faster than Java or Matlab But writing something in C doesn’t guarantee speed It’s up to you to write efficient code Two strategies for optimization Use better algorithm or data structure Always start here! CS 211, CS 482 discuss how to do this Implement algorithm more efficiently...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell.

Page1 / 6

61_pdfsam_cs2022_lectures - (usually Avoid C’s more terse...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online