Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.

Similar presentations


Presentation on theme: "Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics."— Presentation transcript:

1 Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics

2 Unit Learning Objectives TRU-COMP2130 C: Advanced Topics 2 Use pointers for dynamic memory management. Structures and unions Use open(), read(), write(), close(), and FILE* functions to manipulate files. Use user-defined data structures. More coming

3 Unit Contents TRU-COMP2130 C: Advanced Topics 3 1. Pointers and Arrays 2. Input and Output 3. Structures 4. Self Referential structures

4 TRU-COMP2130 C Programming 4 Dynamic memory management #include void *malloc(int size); // allocate size bytes // and return the addr void free(void *); // free the memory space... int *p, n; scanf(“%d”, &n); // space for n-many integer variables. p = (int*)malloc(sizeof(int) * n); p[0] = 10; *(p+1) = 20; *(p+2) = 30; p++; *p = 4;

5 Structures Introduction 5 It is a way to have a single name referring to a group of a related values. Structures provide a way of storing many different values in variables of potentially different types under the same name. its design makes things more compact. Structs are generally useful whenever a lot of data needs to be grouped together Example: a contact group may have {name, address, phone number, and mobile number} TRU-COMP2130

6 Example Introduction 6 struct database { int id_number; int age; float salary; }; int main() { struct database employee; /* There is now an employee variable that has modifiable variables inside it.*/ employee.age = 22; employee.id_number = 1; employee.salary = ; printf(“Employee ID = %d, Age = %d and Salary = %7.2f”, employee.id, employee.age, employee.salary); } TRU-COMP2130

7 2. Structures TRU-COMP2130 C: Advanced Topics 7 User-defined data structure struct student_rcd {// class without methods in Java intstudent_number; charname[128];... };... struct student_rcd record[10], *rp; struct student_rcd test;// how to declare a struct variable test.student_number = 10;// how to access a member print_rcd(test); read_rcd(&test); record[0].student_number = 5;

8 TRU-COMP2130 C: Advanced Topics 8 void print_rcd(struct student_rcd rcd) { printf(“Number: %d\n”, rcd.student_number); printf(“Name: %s\n”, rcd.name); // name is an array. // not &(rcd.name) } void read_rcd(struct student_rcd *rcd) { printf(“Enter number: “); scanf(“%d”, &(rcd->student_number)); // reference rqd printf(“Enter name: “); scanf(“%s”, rcd->name); // name is an array. // not &(rcd->name) }

9 Typedef TRU-COMP2130 C: Advanced Topics 9 typedef int Length; // Now Length is a data type. typedef char *String; // Now String is a data type. typedef struct tnode { /* the tree node: */ char *word; /* points to the text */ int count; /* number of occurrences */ struct tnode *left, *right; /* children */ struct tnode *parent; } Treenode; // Now Treenode is a data type.... Length len, maxlen; Length *lengths[]; String p, lineptr[MAXLINES]; Treenodetnode1;

10 Union TRU-COMP A union is a special data type available in C that enables you to store different data types in the same memory location like structures The main difference is that a union may have many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multi-purpose. #include union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; printf( "data.i : %d\n", data.i); return 0; }

11 Self-Referential Structures TRU-COMP2130 C: Advanced Topics 11 struct tnode { /* the tree node: */ char word[50]; int count; struct tnode *next; /* next child */ }; struct tnode root; root.next = (struct tnode *)malloc(...);

12 Dynamic Memory Allocation TRU-COMP2130 C: Advanced Topics 12 struct Student { char name[128]; int number; Student *next; }; typedef struct Student Student; Student *head = null; // important Student *new, *tmp; new = create_student(); // create a record, read data from the user, // store them into the record add_student(head, new); // add the new record at the end of the list tmp = find_student(head, ); // find the record whose number is.. printf(“%d: %s\n”, tmp->number, tmp->name); // print the record delete_student(head, ); Name Number next Name Number next Name Number next Head null

13 Allocation functions TRU-COMP2130 Introduction 13 Malloc() Calloc() Free()

14 Malloc() TRU-COMP2130 Introduction 14 The name malloc stands for "memory allocation". The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form. Syntax of malloc() ptr=(cast-type*)malloc(byte-size) ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer. Example: ptr=(int*)malloc(100*sizeof(int)); will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory.

15 Calloc() TRU-COMP2130 Introduction 15 The name calloc stands for "contiguous allocation". The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero. Syntax of calloc() ptr=(cast-type*)calloc(n,element-size); This statement will allocate contiguous space in memory for an array of n elements. Example: ptr=(float*)calloc(25,sizeof(float)); This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes.

16 Free() TRU-COMP2130 Introduction 16 Dynamically allocated memory with either calloc() or malloc() does not get return on its own. The programmer must use free() explicitly to release space. syntax of free() free(ptr); This statement cause the space in memory pointer by ptr to be deallocated.

17 realloc TRU-COMP2130 Introduction 17 If the previously allocated memory is insufficient or more than sufficient. Then, you can change memory size previously allocated using realloc(). Syntax of realloc() ptr=realloc(ptr,newsize); Here, ptr is reallocated with size of newsize.

18 TRU-COMP2130 C: Advanced Topics create_student(... ) // create a record, read data from the user, { // store them into the record Student *new = (... )malloc(... ); scanf(“%s”,...) // read name scanf(“%d”,...) // read number new->next = null; // very important; (*new).next = null return...; };... add_student(... head,... new) // add the new record at the end of the list { Student *tmp; if (head == null) // when there is no record yet head = new; else { while((*tmp).next != null) // move to the last record tmp = (*tmp).next; // You cannot use array syntaxes (*tmp).next = new; // because the Student objects were not } // consecutively created. return; }

19 TRU-COMP2130 C: Advanced Topics find_student(... head,... no) // find the record { Student *tmp; tmp = head; while(tmp != null) { if ((... == no) break; tmp =...; }... delete_student(... head,... no) {... }


Download ppt "Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics."

Similar presentations


Ads by Google