COMP26120: Algorithms and Imperative Programming Lecture C2: C - - - PowerPoint PPT Presentation

comp26120 algorithms and imperative programming
SMART_READER_LITE
LIVE PREVIEW

COMP26120: Algorithms and Imperative Programming Lecture C2: C - - - PowerPoint PPT Presentation

COMP26120: Algorithms and Imperative Programming Lecture C2: C - Simple Data Structures Pete Jinks School of Computer Science, University of Manchester Autumn 2011 COMP26120 Lecture C2 1/28 Review What have you learnt about C in the lab?


slide-1
SLIDE 1

COMP26120: Algorithms and Imperative Programming

Lecture C2: C - Simple Data Structures Pete Jinks

School of Computer Science, University of Manchester

Autumn 2011

COMP26120 Lecture C2 1/28

slide-2
SLIDE 2

Review

What have you learnt about C in the lab?

COMP26120 Lecture C2 2/28

slide-3
SLIDE 3

Lecture Outline

Lab 4: Arrays, Strings, Parameters Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc

COMP26120 Lecture C2 3/28

slide-4
SLIDE 4

Lecture C2: You are here

Lab 4: Arrays, Strings, Parameters Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 4/28

slide-5
SLIDE 5

1-D arrays

Moodle theme: Information Representation Moodle: Introduction to Arrays; Arrays Declare fred as an array of 100 characters (0 to 99): char fred [100]; Access: fred[0] or fred[99] or fred[i] etc. No bound-checks: fred[-1] or fred[100] etc.

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 5/28

slide-6
SLIDE 6

1-D arrays

Moodle theme: Information Representation Moodle: Introduction to Arrays; Arrays Declare fred as an array of 100 characters (0 to 99): char fred [100]; Access: fred[0] or fred[99] or fred[i] etc. No bound-checks: fred[-1] or fred[100] etc.

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 5/28

slide-7
SLIDE 7

1-D arrays

Moodle theme: Information Representation Moodle: Introduction to Arrays; Arrays Declare fred as an array of 100 characters (0 to 99): char fred [100]; Access: fred[0] or fred[99] or fred[i] etc. No bound-checks: fred[-1] or fred[100] etc.

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 5/28

slide-8
SLIDE 8

2-D arrays

Moodle: Introduction to Arrays; Multidimensional Arrays Declare fred as a 2-dimensional array of 10 by 10 characters: char fred [10][10]; Access: fred[0][0] or fred[9][9] or fred[i][j] etc.

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 6/28

slide-9
SLIDE 9

2-D arrays

Moodle: Introduction to Arrays; Multidimensional Arrays Declare fred as a 2-dimensional array of 10 by 10 characters: char fred [10][10]; Access: fred[0][0] or fred[9][9] or fred[i][j] etc.

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 6/28

slide-10
SLIDE 10

Strings

Moodle: Strings Declare fred as an initialised string of characters: char fred [] = "hello"; Actually stored with an extra 6th byte = (number) 0 Access: fred[0] or fred[5] or fred[i] etc. printf("%s\n", fred); Q: What will this do: fred[4]= 0;

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 7/28

slide-11
SLIDE 11

Strings

Moodle: Strings Declare fred as an initialised string of characters: char fred [] = "hello"; Actually stored with an extra 6th byte = (number) 0 Access: fred[0] or fred[5] or fred[i] etc. printf("%s\n", fred); Q: What will this do: fred[4]= 0;

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 7/28

slide-12
SLIDE 12

Strings

Moodle: Strings Declare fred as an initialised string of characters: char fred [] = "hello"; Actually stored with an extra 6th byte = (number) 0 Access: fred[0] or fred[5] or fred[i] etc. printf("%s\n", fred); Q: What will this do: fred[4]= 0;

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 7/28

slide-13
SLIDE 13

Pointers (references)

Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers?

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

slide-14
SLIDE 14

Pointers (references)

Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers?

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

slide-15
SLIDE 15

Pointers (references)

Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers?

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

slide-16
SLIDE 16

Pointers (references)

Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers?

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

slide-17
SLIDE 17

Pointers (references)

Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers?

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

slide-18
SLIDE 18

Value Parameters

void increment (int i) { i = i + 1; } . . . int j= 0; increment(j); printf("%d\n", j); Q: What happens?

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 9/28

slide-19
SLIDE 19

Reference Parameters

void increment (int *i) { *i = *i + 1; } . . . int j= 0; increment(&j); printf("%d\n", j); Q: What happens?

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 10/28

slide-20
SLIDE 20

Array Parameters

void increment (int i[]) { i[0] = i[0] + 1; } . . . int j[10]; j[0]= 0; increment(j); printf("%d\n", j[0]); Q: What happens?

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 11/28

slide-21
SLIDE 21

Array Parameters as Pointers

void increment (int *i) { i[0] = i[0] + 1; } . . . int j[10]; j[0]= 0; increment(j); printf("%d\n", j[0]); Q: What happens?

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 12/28

slide-22
SLIDE 22

2-D Arrays as Parameters

void increment (int i[][]) { i[0][0] = i[0][0] + 1; } . . . int j[10][10]; j[0][0]= 0; increment(j); printf("%d\n", j[0][0]); Q: What happens? void increment (int n, int m, int i[n][m]) . . . . . . increment(10, 10, j);

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 13/28

slide-23
SLIDE 23

2-D Arrays as Parameters

void increment (int i[][]) { i[0][0] = i[0][0] + 1; } . . . int j[10][10]; j[0][0]= 0; increment(j); printf("%d\n", j[0][0]); Q: What happens? void increment (int n, int m, int i[n][m]) . . . . . . increment(10, 10, j);

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 13/28

slide-24
SLIDE 24

Strings as Pointers

Declare fred as a pointer to a literal string of characters: char *fred = "hello"; Q: What will this do: fred[4]= 0; arrays/strings are similar to pointers, but they aren’t identical! char *fred = strdup("hello");

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 14/28

slide-25
SLIDE 25

Strings as Pointers

Declare fred as a pointer to a literal string of characters: char *fred = "hello"; Q: What will this do: fred[4]= 0; arrays/strings are similar to pointers, but they aren’t identical! char *fred = strdup("hello");

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 14/28

slide-26
SLIDE 26

Functions used as Parameters

Moodle theme: Program Structuring → Call by reference functions e.g. int compare1 (int a, int b) {return a <= b;} int compare2 (int a, int b) {return a >= b;} void sort (int n, int j[n], int(*comp)(int, int)) { ... comp(j[x],j[y]) ... } . . . int j[10]; . . . sort (10, j, &compare1); sort (10, j, compare2); (man qsort)

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 15/28

slide-27
SLIDE 27

Functions used as Parameters

Moodle theme: Program Structuring → Call by reference functions e.g. int compare1 (int a, int b) {return a <= b;} int compare2 (int a, int b) {return a >= b;} void sort (int n, int j[n], int(*comp)(int, int)) { ... comp(j[x],j[y]) ... } . . . int j[10]; . . . sort (10, j, &compare1); sort (10, j, compare2); (man qsort)

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 15/28

slide-28
SLIDE 28

Functions used as Parameters

Moodle theme: Program Structuring → Call by reference functions e.g. int compare1 (int a, int b) {return a <= b;} int compare2 (int a, int b) {return a >= b;} void sort (int n, int j[n], int(*comp)(int, int)) { ... comp(j[x],j[y]) ... } . . . int j[10]; . . . sort (10, j, &compare1); sort (10, j, compare2); (man qsort)

COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 15/28

slide-29
SLIDE 29

Lecture C2: You are here

Lab 4: Arrays, Strings, Parameters Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 16/28

slide-30
SLIDE 30

Struct

Moodle: Structures Q: What does this Java do? class Fred { public int a; public float b; } Fred sue = new Fred(); sue.a= 0; Similar C (but better version later): struct fred { int a; float b; }; struct fred sue; sue.a= 0;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 17/28

slide-31
SLIDE 31

Struct

Moodle: Structures Q: What does this Java do? class Fred { public int a; public float b; } Fred sue = new Fred(); sue.a= 0; Similar C (but better version later): struct fred { int a; float b; }; struct fred sue; sue.a= 0;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 17/28

slide-32
SLIDE 32

Typedef

Moodle: Structures → the typedef facility We can save some typing by giving a name to struct fred: typedef struct fred Fred; Fred sue; sue.a= 0;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 18/28

slide-33
SLIDE 33

Union

Moodle: Unions union bert { int a; float b; }; union bert anne; anne.a= 0; but a and b occupy the same space!

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 19/28

slide-34
SLIDE 34

Enum

Similar to Java static final int enum {buffersize=10000}; enum traffic lights{green, amber, red, red and amber};

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 20/28

slide-35
SLIDE 35

e.g. Top-Gear race across London

enum vehicle kind {undefined, car, bicycle, bus, speedboat, tube}; struct vehicle { char *model; enum vehicle kind kind; union { /*bicycle - nothing extra */ char *route; /*bus, tube*/ struct {int engine size; float price;} car; int bhp; /* speedboat */ } info; };

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 21/28

slide-36
SLIDE 36

e.g. Top-Gear race across London ctd.

struct vehicle hamster, stig1, stig2, jezzer, captain slow; hamster.kind= bicycle; hamster.model= "Specialized Sirrus Limited"; jezzer.kind= speedboat; jezzer.info.bhp= 225; jezzer.model= "Cougar"; captain slow.kind= car; captain slow.model= "Mercedes GL500"; captain slow.info.car.engine size= 5461; captain slow.info.car.price= 70160.00; stig1.kind= bus; stig1.info.route= "391 Fulham"; stig2.kind= tube; stig2.info.route= "District";

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 22/28

slide-37
SLIDE 37

Array of Struct

We have a struct vehicle that describes a vehicle Q: Declare an array (called vehicles) of 10 of these structs Q: Set vehicles[0]’s kind to car

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 23/28

slide-38
SLIDE 38

Array of Struct

We have a struct vehicle that describes a vehicle Q: Declare an array (called vehicles) of 10 of these structs struct vehicle vehicles[10]; Q: Set vehicles[0]’s kind to car

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 23/28

slide-39
SLIDE 39

Array of Struct

We have a struct vehicle that describes a vehicle Q: Declare an array (called vehicles) of 10 of these structs struct vehicle vehicles[10]; Q: Set vehicles[0]’s kind to car

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 23/28

slide-40
SLIDE 40

Array of Struct

We have a struct vehicle that describes a vehicle Q: Declare an array (called vehicles) of 10 of these structs struct vehicle vehicles[10]; Q: Set vehicles[0]’s kind to car vehicles[0].kind = car;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 23/28

slide-41
SLIDE 41

Malloc

Moodle: Arrays and Pointers; Memory Allocation malloc in C is simliar to new in Java – it gives us memory space for a new variable A few slides back, I said: Java: Fred sue = new Fred(); sue.a= 0; is similar to C: struct fred sue; sue.a= 0; Actually, it is closer to: typedef struct fred Fred; Fred *sue = (Fred *) malloc (sizeof(Fred)); (*sue).a= 0;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 24/28

slide-42
SLIDE 42

Malloc

Moodle: Arrays and Pointers; Memory Allocation malloc in C is simliar to new in Java – it gives us memory space for a new variable A few slides back, I said: Java: Fred sue = new Fred(); sue.a= 0; is similar to C: struct fred sue; sue.a= 0; Actually, it is closer to: typedef struct fred Fred; Fred *sue = (Fred *) malloc (sizeof(Fred)); (*sue).a= 0;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 24/28

slide-43
SLIDE 43

Malloc

Moodle: Arrays and Pointers; Memory Allocation malloc in C is simliar to new in Java – it gives us memory space for a new variable A few slides back, I said: Java: Fred sue = new Fred(); sue.a= 0; is similar to C: struct fred sue; sue.a= 0; Actually, it is closer to: typedef struct fred Fred; Fred *sue = (Fred *) malloc (sizeof(Fred)); (*sue).a= 0;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 24/28

slide-44
SLIDE 44

Notes

  • man malloc says:

“[...] the value returned is a pointer to the allocated memory [...] or NULL if the request fails.” Fred *sue = (Fred *) malloc (sizeof(Fred)); if (sue==NULL) ...

  • Some notation:

instead of: (*sue).a= 0; we can write: sue->a= 0;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 25/28

slide-45
SLIDE 45

Notes

  • man malloc says:

“[...] the value returned is a pointer to the allocated memory [...] or NULL if the request fails.” Fred *sue = (Fred *) malloc (sizeof(Fred)); if (sue==NULL) ...

  • Some notation:

instead of: (*sue).a= 0; we can write: sue->a= 0;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 25/28

slide-46
SLIDE 46

Array of Pointer to Struct

Moodle: Arrays of Pointers Q: Declare an array of 10 pointers to struct vehicle Q: Use malloc to create the space(s) for vehicles Q: Set vehicles[0]’s kind to car

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 26/28

slide-47
SLIDE 47

Array of Pointer to Struct

Moodle: Arrays of Pointers Q: Declare an array of 10 pointers to struct vehicle typedef struct vehicle* ptr2vehicle; ptr2vehicle vehicles[10]; Q: Use malloc to create the space(s) for vehicles Q: Set vehicles[0]’s kind to car

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 26/28

slide-48
SLIDE 48

Array of Pointer to Struct

Moodle: Arrays of Pointers Q: Declare an array of 10 pointers to struct vehicle typedef struct vehicle* ptr2vehicle; ptr2vehicle vehicles[10]; Q: Use malloc to create the space(s) for vehicles Q: Set vehicles[0]’s kind to car

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 26/28

slide-49
SLIDE 49

Array of Pointer to Struct

Moodle: Arrays of Pointers Q: Declare an array of 10 pointers to struct vehicle typedef struct vehicle* ptr2vehicle; ptr2vehicle vehicles[10]; Q: Use malloc to create the space(s) for vehicles for (int i= 0; i<10; i++) vehicles[i]= (struct vehicle*) malloc(sizeof(struct vehicle)); Q: Set vehicles[0]’s kind to car

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 26/28

slide-50
SLIDE 50

Array of Pointer to Struct

Moodle: Arrays of Pointers Q: Declare an array of 10 pointers to struct vehicle typedef struct vehicle* ptr2vehicle; ptr2vehicle vehicles[10]; Q: Use malloc to create the space(s) for vehicles for (int i= 0; i<10; i++) vehicles[i]= (struct vehicle*) malloc(sizeof(struct vehicle)); Q: Set vehicles[0]’s kind to car

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 26/28

slide-51
SLIDE 51

Array of Pointer to Struct

Moodle: Arrays of Pointers Q: Declare an array of 10 pointers to struct vehicle typedef struct vehicle* ptr2vehicle; ptr2vehicle vehicles[10]; Q: Use malloc to create the space(s) for vehicles for (int i= 0; i<10; i++) vehicles[i]= (struct vehicle*) malloc(sizeof(struct vehicle)); Q: Set vehicles[0]’s kind to car vehicles[0]->kind= car;

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 26/28

slide-52
SLIDE 52

C doesn’t have Garbage Collection

man 3 free says: “void free(void *ptr) frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc [...]” man valgrind says: “valgrind - a suite of tools for debugging and profiling programs [...] Valgrind is typically invoked as follows: valgrind program args This runs program (with arguments args) under Valgrind using the Memcheck tool. Memcheck performs a range of memory-checking functions, including detecting accesses to uninitialised memory, misuse of allocated memory (double frees, access after free, etc.) and detecting memory leaks. [...]”

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 27/28

slide-53
SLIDE 53

C doesn’t have Garbage Collection

man 3 free says: “void free(void *ptr) frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc [...]” man valgrind says: “valgrind - a suite of tools for debugging and profiling programs [...] Valgrind is typically invoked as follows: valgrind program args This runs program (with arguments args) under Valgrind using the Memcheck tool. Memcheck performs a range of memory-checking functions, including detecting accesses to uninitialised memory, misuse of allocated memory (double frees, access after free, etc.) and detecting memory leaks. [...]”

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 27/28

slide-54
SLIDE 54

Lecture Review

Lab 4: Arrays, Strings, Parameters Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc Arrays: 1-D + multi-D, Strings Pointers: *, &, Reference, Array + Function Parameters Data structures: Struct, Union, Enum Simpler type-names: Typedef Pointers (again): Malloc C doesn’t Garbage Collect: Free, Valgrind

COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 28/28