Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a wrappered function? –How to assess efficiency. –What is a clean interface?

Similar presentations


Presentation on theme: "C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a wrappered function? –How to assess efficiency. –What is a clean interface?"— Presentation transcript:

1 C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a wrappered function? –How to assess efficiency. –What is a clean interface? –How can structures contain other structures? –What is a linked list?

2 Error checking in programs A good programmer always checks file reading, user input and memory allocation (see later) for errors. Nothing convinces a user of your program that you're an idiot faster than it crashing when they type "the wrong thing". Take some action to avert the error even if that action is stopping the program. It is best to print errors to the stderr stream. fprintf (stderr,"There is an error!\n");

3 Wrappered function Isn't it pretty boring to write an error check every time you try a malloc. Most of the time, after all, you just say "out of memory" and exit. It seems like lots of effort to write the same bit of code every time. The solution is to write your own "wrappered" malloc. You might want to "wrapper" other functions (though this is less common).

4 safe_malloc #include void *safe_malloc (size_t); /* Error trapping malloc wrapper */ void *safe_malloc (size_t size) /* Allocate memory or print an error and exit */ { void *ptr; ptr= malloc(size); if (ptr == NULL) { fprintf (stderr, "No memory line:%d file:%s\n", __LINE__, __FILE__); exit(-1); } return ptr; }

5 The O-notation The O-notation is used to approximate the efficiency of an algorithm. If we say that the execution time is O(f(n)) we mean: Note therefore that: though generally we specify the execution time in the strongest possible terms

6 O-notation (getting the feel) nlg nn lg nn 1.25 n2n2 10011 1646432256 82,0481,02465,536 4,0961249,15232,76816,777,216 65,536161,048,5651,048,4764,294,967,296 1,048,4762020,969,52033,554,4321,099,301,922,576 16, 775,616 24 402, 614,784 1,073, 613,825 281,421,292, 179,456

7 O-notation Inserting into an array – an O(n) operation

8 The Travelling Salesman The most famous O(n!) problem is the travelling salesman problem A salesman must visit all of n cities – what is his shortest path to do so? At the moment we must check all of his possible routes (n!/2 – we can divide by 2 due to symmetry) We will hear more about the TSP later.

9 Other efficiency considerations Fewer characters typed is not more efficient. Sometimes you can trade memory for execution time (e.g. the sieve) Allocating memory (with pointers or arrays) is slow. File access is very slow. Consider your algorithms carefully – see where gains can be made.

10 Good programming practice "a clean interface" A good programmer makes their code useful to other programmers. The best way to do this is to write useful functions which are easy to use. If your functions are good then there shouldn't be _too_ many arguments passed to them. Think about "what do I need to pass to this function" and "what do I need back from it". If you have written a clean interface then your functions will almost explain themselves.

11 What are the rules for writing functions in a "clean interface" The functions should be simple - don't try to force your function to do too much - nor make them too limited. In the is_prime examples, it would be silly to write a function which found the lowest prime from 123 to 142 But it might not be unreasonable to write one which took two numbers and found the lowest prime in that range

12 Example structure pay.h Header file - enums, structs, prototypes pay.cpp #include "pay.h" int main() Get user input Call appropriate routine fileio.cpp #include "pay.h" Read records when called Writes records Make backup copies update.cpp #include "pay.h" Type in a new file for a new lecturer Change file for existing lecturer printout.cpp #include "pay.h" Print a cheque run for all lecturers Print records of individual lecturers for inspection

13 By using structs, we can make our functions look simpler Sometimes we need to pass a LOT of information to a function. void write_record (FILE *fptr, char name[], int wage, int hire_date) /* Function to write info about a lecturer */ { } void write_record (FILE *fptr, LECTURER this_lecturer) /* Function to write info about a lecturer */ { } Nicer to bundle it as a struct - and we can add stuff later

14 Functions should be "consistent" If you write a lot of similar functions it is best to make them work the "same way" int write_record (char fname[],LECTURER lect) /* Return -1 for FAIL 0 for success */ { } int add_record (LECTURER lect, char fname[]) /* Return 0 for FAIL 1 for success */ { } The second function is perverse given the first Another programmer reading your code will be justified in anything short of actual bodily harm if your code works like this.

15 Functions should be predictable Don't make your function change arguments it doesn't NEED TO. Unless your function is explicitly supposed to change arrays, DON'T change the array. FILE *fptr; char fname[]= "file.txt"; fptr= fopen (fname, "r"); if (fptr == NULL) { printf ("Can't open %s\n",fname); return -1; } Wouldn't you be annoyed if fopen had unexpectedly changed what was in fname?

16 Structs which contain themselves Sometimes programmers want structs in C to contain themselves. For example, we might design an electronic dictionary which has a struct for each word and we might want to refer to synonyms which are also word structures. word1:run synonym1 synonym2 word2: sprint synonym1 synonym2 word3: jog synonym1 synonym2

17 How structs can contain themselves Clearly a struct cannot literally contain itself. But it can contain a pointer to the same type of struct struct silly_struct { /* This doesn't work */ struct silly_struct s1; }; struct good_struct { /* This does work */ struct *good_struct s2; };

18 The linked list - a common use of structs which contain themselves Imagine we are reading lines from a file but don't know how many lines will be read. We need a structure which can extend itself. This is known as a linked list. By passing the value of the head of the list to a function we can pass ALL the information.

19 How to set up a linked list typedef struct list_item { information in each item struct list_item *nextptr; } LIST_ITEM; This structure (where information is what you want each "node" of your linked list to contain). It is important that the nextptr of the last bit of the list contains NULL so that you know when to stop. NULL head of list

20 Linked list pros & cons Pro: Can easily add items to the head or middle of the list (tough with array) - good for ordered lists Pro:We can make the list as big as we like Con: Complicated for a beginner to program (don't underestimate this) Con: Hard to find nth element Con: Slightly larger since we store pointer and information More will be said on this useful technique next lecture.


Download ppt "C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a wrappered function? –How to assess efficiency. –What is a clean interface?"

Similar presentations


Ads by Google