1 / 22

Programming Fundamentals using C Standard Library

Programming Fundamentals using C Standard Library. LETI Group. Training objectives. After this lecture you will be able to: Debug an applications Analyze error. Learning approach. The following are strongly suggested for a better learning and understanding of this course:

berne
Download Presentation

Programming Fundamentals using C Standard Library

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Fundamentals using CStandard Library LETI Group

  2. Training objectives • After this lecture you will be able to: • Debug an applications • Analyze error

  3. Learning approach • The following are strongly suggested for a better learning and understanding of this course: • Noting down the key concepts in the class • Analyze all the examples / code snippets provided • Study and understand the self study topics • Completion and submission of all the assignments, on time • Completion of the self review questions in the lab guide • Study and understand all the artifacts including the reference materials / e-learning / supplementary materials specified • Completion of the project (if application for this course) on time inclusive of individual and group activities • Taking part in the self assessment activities • Participation in the doubt clearing sessions

  4. Training agenda • Debuging in visual studio 2008 • Analyze some common C error

  5. Debuging in visual studio 2008 • Debuging options

  6. Debuging in visual studio • Debuging windows • Breakpoints • Output  • Watch  • Autos  • Locals  • Call Stack • Threads  • Disassembly  • Registers  • Memory

  7. Debuging in visual studio • Using Breakpoints F5: Start Debugging/Continue Ctrl+Alt+Break: Break All  Shift+F5: Stop Debugging  Ctrl+Shift+F5: Restart F11: Step Into F10: Step Over Shift+F11: Step Out

  8. Q & A

  9. Analyzing error

  10. Switch error • int x = 2; • switch(x) { • case 2: • printf("Two\n"); • case 3: • printf("Three\n"); • }

  11. Using '=' instead of '==' • int x = 5; • if ( x = 6 ) • printf("x equals 6\n");

  12. Forgetting to put an ampersand (&) on arguments • int x; • char * st = malloc(31); • scanf("%d", &x); /* & required to pass address to scanf() */ • scanf("%30s", st); /* NO & here, st itself points to variable! */

  13. Using the wrong format for operand • C compilers do not check that the correct format is used for arguments of a scanf() call. The most common errors are using the %f format for doubles (which must use the %lf format) and mixing up %c and %s for characters and strings.

  14. Size of arrays • Arrays in C always start at index 0. This means that an array of 10 integers defined as: • int a[10]; • has valid indices from 0 to 9 not 10! It is very common for students go one too far in an array. This can lead to unpredictable behavior of the program.

  15. Integer division • Unlike Pascal, C uses the / operator for both real and integer division. It is important to understand how C determines which it will do. If both operands are of an integal type, integer division is used, else real division is used. For example: • double half = 1/2; • This code sets half to 0 not 0.5! Why? Because 1 and 2 are integer constants. To fix this, change at least one of them to a real constant. • double half = 1.0/2; • If both operands are integer variables and real division is desired, cast one of the variables to double (or float). • int x = 5, y = 2; • double d = ((double) x)/y;

  16. Some more errors • Loop errors • int x = 5; • while( x > 0 ); • x--; • Not using prototype • double x = sqrt(2);

  17. Not initializing pointers #include <string.h> int main() { char * st; /* a pointer to a char or char array */ strcpy(st, "abc"); return 0; } what char array does st point to ????

  18. String errors • Confusing character and string • Not null terminating strings • Not leaving room for the null terminator • char ch = 'A'; /* correct */ • char ch = "A"; /* error */ • const char * st = "A"; /* correct */ • const char * st = 'A'; /* error */ char str[30]; char * copy_str = malloc( strlen(orig_str) + 1); strcpy(copy_str, orig_str);

  19. String comparing error • char st1[] = "abc"; • char st2[] = "abc"; • if ( st1 == st2 ) • printf("Yes"); • else • printf("No"); Pointer adress not value if ( strcmp(st1,st2) == 0 ) printf("Yes"); else printf("No");

  20. Input/Output Errors • Using fgetc(), etc. Incorrectly • Using feof() incorrectly This program will print out the last line of the input file twice. Why? After the last line is read in and printed out, feof() will still return 0 (false) and the loop will continue. The next fgets() fails and so the line variable holding the contents of the last line is not changed and is printed out again.After this, feof() will return true (since fgets() failed) and the loop ends. #include <stdio.h> int main() { FILE * fp = fopen("test.txt", "r"); char line[100]; while(fgets(line, sizeof(line), fp) != NULL ) fputs(line, stdout); fclose(fp); return 0; } int count_line_size( FILE * fp ) { char ch; int cnt = 0; while( (ch = fgetc(fp)) != EOF && ch != '\n') cnt++; returan cnt; }

  21. Memory errors • Invalid Memory Access • Memory leaks • Mismatched Allocation/Deallocation • Missing allocation • Uninitialized Memory Access • Cross Stack Access char *pStr = (char*) malloc(25); free(pStr); strcpy(pStr, ”code for food”); main() { int *p; ------- CreateThread(., thread #1, .); // Stack Owned CreateThread(., thread #2, .); ------- } Thread #1 { int q[1024]; p = q; q[0] = 1; } Thread #2 { *p = 2; // Stack Cross Accessed } char *pStr = (char*) malloc(512); return; char *s = (char*) malloc(5); delete s; char* pStr = (char*) malloc(20); free(pStr); free(pStr); // results in an invalid deallocation char *pStr = (char*) malloc(512); char c = pStr[0]; // the contents of pStr were not initialized void func() { int a; int b = a * 4; // uninitialized read of variable a }

  22. Q&A

More Related