Lec14 - CS240 Programming in C Lecture 14 Errors Sunday April 3 2011 Errors We've already seen a number of instances where unexpected(and uncaught

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

View Full Document Right Arrow Icon
CS240: Programming in C Lecture 14: Errors Sunday, April 3, 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Errors We’ve already seen a number of instances where unexpected (and uncaught) errors can take place: Memory buffer overflow/underflow unintended casts misuse of sizeof structure field alignment pointer arithmetic dangling references 2 Sunday, April 3, 2011
Background image of page 2
Example 1 3 int x = 2; switch (x) { case 2: printf(“Two\n”); case 3: printf(“Three\n”); } This prints Two Three Why? Sunday, April 3, 2011
Background image of page 3

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

View Full DocumentRight Arrow Icon
Example 2 4 int x = 5; if (x = 6) printf(“x equals 6”\n); This prints x equals 6 Why? Sunday, April 3, 2011
Background image of page 4
Example 2 4 int x = 5; if (x = 6) printf(“x equals 6”\n); This prints x equals 6 Why? The assignment x = 6 in the conditional test sets x to 6, and returns a non-zero value which executes the branch Sunday, April 3, 2011
Background image of page 5

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

View Full DocumentRight Arrow Icon
Example 3 Not supplying address of variable in scanf where input should be stored. Array addressing starts at 0 Overloaded division double half = 1/2; sets half to 0 not 0.5 since 1 and 2 are integer constants. need to cast to get correct behavior 5 int x = 5, y = 2; double d = ((double) x) / y; Sunday, April 3, 2011
Background image of page 6
Common Semantic Errors 6 int x = 5; while (x > 0); x-- This is an infinite loop. Why? Sunday, April 3, 2011
Background image of page 7

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

View Full DocumentRight Arrow Icon
Common Semantic Errors 6 int x = 5; while (x > 0); x-- This is an infinite loop. Why? The semicolon after the while defines the statement to repeat -- it’s simply the null statement. Sunday, April 3, 2011
Background image of page 8
Common Semantic Errors 7 #include <string.h> int main () { char * st; strcpy(st, “abc”); return 0; } Generates an error when executed. Why? Sunday, April 3, 2011
Background image of page 9

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

View Full DocumentRight Arrow Icon
Common Semantic Errors 7 #include <string.h> int main () { char * st; strcpy(st, “abc”); return 0; } Generates an error when executed. Why? Space for st has not been allocated. Change declaration to char st[20] Sunday, April 3, 2011
Background image of page 10
Common Semantic Errors 8 char st1[] = “abc”; char st2[] = “abc”; if (st1 == st2) printf(“Yes”); else printf(“No”) char str[30] only has room for 29 (not 30) data characters since the last character must be null char * copy_str = malloc(strlen(orig_str) + 1); strcpy(copy_str, orig_str) Sunday, April 3, 2011
Background image of page 11

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

View Full DocumentRight Arrow Icon
Common Semantic Errors 9 int count_line_size (FILE * fp) {
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2012 for the course CS 240 taught by Professor Rego during the Spring '08 term at Purdue University-West Lafayette.

Page1 / 28

Lec14 - CS240 Programming in C Lecture 14 Errors Sunday April 3 2011 Errors We've already seen a number of instances where unexpected(and uncaught

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

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