<<

Unix Structures and Pointers + GNU HTML Python Comp-206 : Introduction to Software Systems Week 7

Joseph Vybihal S C o O f t w M Computer Science a P r e 2

0 S 6 y

s McGill University - t e J m o s s e p h

V y b i h a l

2 1 0 0 7 Announcements Unix C + GNU • Midterm exam content HTML Perl – Includes lectures: Week 1 to Week 7 (half) Python – 1 hour exam • 3 questions – Programming (1 or 2) – Syntax precision (0 to 1) – Definition (0 to 1) S

C • Programming and syntax based (>95% accuracy) o O f t w M a P r – Topics e 2

0 S 6 y s - t • Unix command-line commands e J m o s s e p • Bash programming h

V y

b • C programming i h a l

2 • About the Unix operating system / history 2 0 0 7 Unix C + GNU HTML Perl Python

Part 1 S C o O f

t C Structures w M a P r

e 2

0 S 6 y

s - t

e J m o s s e p h

V y b i h a l

2 3 0 0 7 Unix Structures and Unions C + GNU HTML Perl Python • Structures are a data type composed of several other data types. – Think of it as a container, a variable that has variables inside it. • You can define new structures using the struct keyword.

struct course_record { struct TYPENAME { S C o O f int number_of_students; FIELDS; t w M a

P char name_professor[100]; } VARNAME; r e 2

0 S

6 char location_building[100]; y s - t e J int location_room; m o s s e p } cs206; h

V y b i h a l

2 4 0 0 7 Unix Using a structure C + GNU HTML Perl Python • To use a structure, you need to instantiate a copy of it. • All you need to do is to declare the variable for the instance. struct course cs206; • You can then fill it with data. cs206.number_of_student = 60; cs206.name_professor = “Alex”; S

C cs206.location_building = “MacDonald”; o O f t w M cs206.location_room = 328; a P r e 2

0 S • With structures, you can declare the variable and initialize it 6 y s - t e J with data in one command. m o s s e p

h struct course cs206 = {60, “Alex”, “MacDonald”,

V 328}; y b i h a l

2 5 0 0 7 Unix typedef and struct C + GNU HTML Perl Python • You can use typedef to define the structure as a new type.

typedef struct course { int number_of_students; char name_professor[100]; char location_building[100]; int location_room; S C } TYPENAME; o O f t w M a P r e 2

0 S • When creating a variable of this type, you no longer need to 6 y s - t e J specify the struct keyword. m o s s e p h

V

y TYPENAME cs206; b i h a l

2 6 0 0 7 Unix Question C + GNU HTML Perl Python • How to build an array of student structures? – And use it? S C o O f t w M a P r

e 2

0 S 6 y

s - t

e J m o s s e p h

V y b i h a l

2 7 0 0 7 Unix Answer C + GNU HTML Perl struct student_rec Python { char name[100]; int age; float gpa; } students[50]; S C o O f

t students[2].age = 20; w M a P r e 2

0 S 6 y s - gets(student[2].name); t e J m o s s e p h

V for(i=0;i<50;i++) printf(“%s”,students[i].name); y b i h a l

2 8 0 0 7 Unix Polymorphism in C C + GNU HTML Perl Python union TYPENAME { FIELDS; } VAR; S

C union multiple o O f t w M { How does this look in RAM? a P r e 2

0 S int x; 6 y s - t e J m o char y; s s e p

h float ;

V y

b } z; i h a l

2 9 0 0 7 Unix Question C + GNU HTML Perl Python • How can we build a polymorphic with: – Students – Teachers – Staffs S C o O f t w M a P r e 2

0 S 6 y s - t e J m o s s e p h

V y b i h a l

2 10 0 0 7 Unix Answer C + GNU HTML Perl struct teacher_rec { .... }; Python struct stud_rec { ... }; struct staff_rec { ... };

union school_rec { struct teacher_rec teacher; S C o O f

t struct stud_rec student; w M a P r

e struct staff_rec staff; 2

0 S 6 y s - } school[100]; t e J m o s s e p h

V y b i h

a How can we tell the difference between records? l

2 11 0 0 7 Unix C + GNU HTML Perl Python

Part 2 S C o O f

t Enumerated Types w M a P r

e 2

0 S 6 y

s - t

e J m o s s e p h

V y b i h a l

2 12 0 0 7 Unix Enumerated Types C + GNU HTML Perl Python • Enumerated types : contain a list of constants that can be addressed in integer values. – enum days {monday, tuesday, wednesday, thursday, friday, saturday, sunday}; • As with arrays first enumerated name has index value 0. – So monday has value 0, tuesday 1, ... • We can also override the 0 start value: S C o O f – t enum days {monday = 1, tuesday, wednesday, w M a P

r thursday, friday, saturday, sunday}; e 2

0 S 6 y

• s Or simply assign different numerical values: - t e J m o

s –

s enum days {monday = 10, tuesday = 20, e p

h wednesday = 30, thursday = 40, friday = 50,

V

y saturday = 60, sunday = 0}; b i h a l

2 13 0 0 7 Unix Using Enumerations C + GNU HTML Perl Python • Creating a variable of an enumeration is similar to a structure: – enum days week1; • If you typedef an , you can S C o use it without the enum keyword. O f t w M a P r –

e {monday = 1,

2 typedef enum days

0 S 6 y s

- tuesday, wednesday, thursday, friday, t e J m o s

s saturday, sunday}; e p h

V –

y days week1; b i h a l

2 14 0 0 7 Unix Example C + GNU HTML Perl Python • Defining an array with enumerated days and then incrementing through the array

• How is this similar to integer indexes? S C o O f t w M a P r e 2

0 S 6 y s - t e J m o s s e p h

V y b i h a l

2 15 0 0 7 Unix Answer C + GNU HTML Perl Python days array[7];

for(i=monday; i<=sunday; i++) array[i]; S C o O f t w M a P r e 2

0 S 6 y s - t e J m o s s e p h

V y b i h a l

2 16 0 0 7 Unix C + GNU HTML Perl Python

Part 3 S C o O f

t Dynamic Memory w M a P r

e 2

0 S 6 y

s - t

e J m o s s e p h

V y b i h a l

2 17 0 0 7 Unix Dynamic Memory Allocation C + GNU HTML Perl • The malloc() function allocates a block of memory Python and returns a pointer to that allocated memory. – void *malloc(size_t size); – void *calloc(int repeat, size_t size); • The size of the block must be specified. • That block memory is not initialized. S C o O f t w M – It will contain whatever is currently in memory. a P r e 2

0 S 6 y • Be careful not to access memory outside what you s - t e J m o s s e allocated. p h

V – y Nothing will prevents you from accessing outside that b i h

a block of memory. l

2 18 0 0 7 Unix Using the blocks of memory C + GNU HTML Perl Python • Both malloc and calloc return a void pointer (void *). • In C, you use a void* when return a generic pointer. • This generic block of memory must be cast before it can be used. S C o O f t w M int *a = (int *) malloc( sizeof(int) * 40 ); a P r e 2

0 S 6 y • The sizeof() function simplifies the allocation of s - t e J m o s

s memory by calculating the size of the provided data e p h

V type. y b i h a l

2 19 0 0 7 Unix Deallocating Memory C + GNU HTML Perl Python • The free() function releases the specified memory space. – void free(void *ptr); • The specified memory must have been returned by a previous call to malloc(), calloc() or realloc(). S C o – Otherwise, undefined behaviour occurs. O f t w M a P r

e •

2 Not releasing memory after finishing with it can

0 S 6 y s - t e J create memory leaks. m o s s e p

h – This can be an especially serious problem if you

V y

b continually allocate memory. i h a l

2 20 0

0 There is no garbage collector in C… 7 Unix Example C + GNU HTML Perl Python • Building my own array… • Building my own string… – What about strdup()? • Building my own struct... S C o O f t w M a P r e 2

0 S 6 y s - t e J m o s s e p h

V y b i h a l

2 21 0 0 7 Unix Answers C + GNU HTML Perl Python int *p = calloc(10,sizeof(int));

char *s = malloc(100);

struct student_rec *ptr = malloc(sizeof(struct student_rec)); S C o O f t w M a P r e 2

0 S 6 y s - t e J m o s s e p h

V y b i h a l

2 22 0 0 7 Unix C + GNU HTML Perl Python

Part 4 S C o O f

t Data Structures w M a P r

e 2

0 S 6 y

s - t

e J m o s s e p h

V y b i h a l

2 23 0 0 7 Unix Data Structures in C C + GNU HTML Perl Python

• To better understand the importance of pointers, let's take a look at two data structures. S C o O f t w M – Linked List a P r e 2

0 S 6 y – Binary Tree s - t e J m o s s e p h

V y b i h a l

2 0 0 7 Unix Linked List C + GNU HTML Perl Python Head

8 6 10 3 S C o O f t w M a P r

e 2

0 S null 6 y

s - t

e J m o s s e p h

V y b i h a l

2 0 0 7 Unix Link List Node C + GNU HTML Perl Python

/* Node for the link list */ typedef struct ll_node { int value; S C o O f t w M struct ll_node* next; a P r e 2

0 S } llnode, linkedlist; 6 y s - t e J m o s s e p h

V typedef names: llnode and linkedlist y b i h a l

2 0 0 7 Unix Creating a node C + GNU HTML Perl Python /* Create a link list */ linkedlist* createLinkedList() {

llnode* head;

// Our implementation of linklist has a // dummy node at the head. S C o head = (llnode *)malloc(sizeof(llnode)); O f t w M

a head->value = 0; P r e 2

0 S head->next = NULL; 6 y s - t e J m o s s e p return head; h

V

y } b i h a l

2 0 0 7 Unix Adding a Node C + GNU HTML /* Add a value to the linklist. */ Perl void addToLinkedList(linkedlist* list, int value) { Python llnode* freeSpot; llnode* newNode;

// Find a free spot at the end to add the value freeSpot = list; while(freeSpot->next != NULL) { S C freeSpot = freeSpot->next; o O f t w M } a P r

e 2

0 S 6 y

s -

t newNode = (llnode *)malloc(sizeof(llnode));

e J m o s s

e newNode->value = value; p h

newNode->next = NULL; V y

b freeSpot->next = newNode; i h a

l }

2 0 0 7 Unix Pretty Print C + GNU HTML Perl Python /* Pretty print the list. */ void printLinkedList(linkedlist* list) {

if (list->next != NULL) { printf("Content of list is :"); printLLNode(list->next); } else { S C o O

f printf("List is empty."); t w M a P } r e 2

0 S 6 y s - t e J m o printf("\n"); s s e p h

V y

b } i h a l

2 0 0 7 Unix Printing A Node C + GNU HTML Perl Python

void printLLNode(llnode* my_node) {

printf(" %i ", my_node->value); if (my_node->next != NULL) { S C o printLLNode(my_node->next); O f t w M

a } P r e 2

0 S

6 } y s - t e J m o s s e p h

V y b i h a l

2 0 0 7 Unix head Binary Tree C + GNU HTML Perl 8 Python

6 10 S C o O f t w M a P r

e 2

0 S 6 y

s - t

e J m o

s null s e null p h

3 V y b i h a l

2 0 0 7 Unix Binary Tree Node C + GNU HTML Perl Python /* Node in our binary tree */ typedef struct bt_node { char* value; struct bt_node* left; S C o O f t w M struct bt_node* right; a P r e 2

0 S } btnode, binarytree; 6 y s - t e J m o s s e p h

V y b i h a l

2 0 0 7 Unix Creating the Tree C + GNU HTML Perl Python

binarytree* createBinaryTree(char* value) {

return createBTNode(value); S C o O f t w

M } a P r e 2

0 S 6 y s - t e J m o s s e p h

V y b i h a l

2 0 0 7 Unix Creating a Node C + GNU HTML Perl Python binarytree* createBTNode(char* value) {

btnode* new_node;

new_node = (btnode*)malloc(sizeof(btnode)); new_node->value = (char *)malloc(strlen(value) + 1); new_node->left = NULL; S

C new_node->right = NULL; o O f t w M a P r strcpy(new_node->value, value);

e 2

0 S 6 y

s - t

e J return new_node; m o s s e

p } h

V y b i h a l

2 0 0 7 Unix Adding a Node C + GNU HTML Perl void addToBinaryTree(binarytree* tree, char* value) { Python // In order travel of the tree, until we hit a left. if (strcmp(tree->value, value) > 0) { if (tree->left != NULL) { addToBinaryTree(tree->left, value); } else { tree->left = createBTNode(value); }

S } else { C o O f t

w if (tree->right != NULL) { M a P r addToBinaryTree(tree->right, value);

e 2

0 S 6

y } else {

s - t

e J tree->right = createBTNode(value); m o s s e } p h

V } y

b } i h a l

2 0 0 7 Unix Freeing the Tree C + GNU HTML Perl void unallocateBinaryTree(binarytree* tree) { Python if (tree == NULL) return;

// First dellocate the child nodes. unallocateBinaryTree(tree->left); unallocateBinaryTree(tree->right); S C o O f t w

M // Deallocate the node's content a P r

e // and the node itself 2

0 S 6 y s

- free(tree->value); t e J m o s

s free(tree); e p h

V y b

i } h a l

2 0 0 7 Unix C + GNU HTML Perl Python

Part 4 S C o O f

t At Home w M a P r

e 2

0 S 6 y

s - t

e J m o s s e p h

V y b i h a l

2 37 0 0 7 Unix Try This C + GNU HTML Perl Try to implement the linked list and tree code demonstrated Python in this lecture. Write a main program that would ask a user to input values that would be added to these data structures. Then have the user search the data structures.

Do this as two different programs, one for linked lists and the other for trees. S C o O f t w M a P r e 2

0 S 6 y s - t e J m o s s e p h

V y b i h a l

2 38 0 0 7 Unix Try this C + GNU HTML Perl Python • Write a program that reads a text file into a 2D array and then asks the user to input two words. Then, from within another function, scan the 2D array for the first word and replace it by the second word. S C o O f t w M a P r • Create the two versions of the swap e 2

0 S 6 y s - t program. The one with and the one without e J m o s s e p pointers. h

V y b i h a l

2 39 0 0 7