programming fundamentals using c standard library

22
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4 Programming Fundamentals using C Standard Library LETI Group

Upload: berne

Post on 11-Jan-2016

42 views

Category:

Documents


0 download

DESCRIPTION

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: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Programming Fundamentals using C

Standard Library

LETI Group

Page 2: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Training objectives

After this lecture you will be able to:Debug an applicationsAnalyze error

Page 3: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

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

Page 4: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Training agenda

Debuging in visual studio 2008 Analyze some common C error

Page 5: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Debuging in visual studio 2008

Debuging options

Page 6: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Debuging in visual studio

Debuging windowsBreakpointsOutput Watch Autos Locals Call StackThreads Disassembly Registers Memory

Page 7: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

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

Page 8: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Q & A

Page 9: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Analyzing error

Page 10: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Switch error

int x = 2; switch(x) { case 2: printf("Two\n"); case 3: printf("Three\n"); }

Page 11: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Using '=' instead of '=='

int x = 5; if ( x = 6 ) printf("x equals 6\n");

Page 12: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

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! */

Page 13: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

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.

Page 14: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

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.

Page 15: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

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;

Page 16: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Some more errors

Loop errors int x = 5; while( x > 0 ); x--;

Not using prototype double x = sqrt(2);

Page 17: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

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 ????

Page 18: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

String errors

Confusing character and string

Not null terminating strings Not leaving room for the null terminator

char str[30];char * copy_str = malloc( strlen(orig_str) + 1);strcpy(copy_str, orig_str);

char ch = 'A'; /* correct */char ch = "A"; /* error */const char * st = "A"; /* correct */const char * st = 'A'; /* error */

Page 19: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

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");

Page 20: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Input/Output Errors

Using fgetc(), etc. Incorrectly Using feof() incorrectly

#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;}

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.

Page 21: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

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”);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 initializedvoid func(){ int a; int b = a * 4;

// uninitialized read of variable a }

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 }

Page 22: Programming Fundamentals using C Standard Library

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Q&A