1 / 41

C: Advanced Topics

C: Advanced Topics. Fall 2012 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University. Course Objectives. The better knowledge of computer systems, the better programing. Course Contents. Introduction to computer systems: B&O 1

Télécharger la présentation

C: Advanced Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C: Advanced Topics Fall 2012 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

  2. Course Objectives • The better knowledge of computer systems, the better programing. Data Representation

  3. Course Contents • Introduction to computer systems: B&O 1 • Introduction to C programming: K&R 1 – 4 • Data representations: B&O 2.1 – 2.4 • C: advanced topics: K&R 5.1 – 5.10, 6 – 7 • Introduction to IA32 (Intel Architecture 32): B&O 3.1 – 3.8, 3.13 • Compiling, linking, loading, and executing: B&O 7 (except 7.12) • Dynamic memory management – Heap: B&O 9.9.1 – 9.9.2, 9.9.4 – 9.9.5, 9.11 • Code optimization: B&O 5.1 – 5.6, 5.13 – 5.15 • Memory hierarchy, locality, caching: B&O 5.12, 6.1 – 6.3, 6.4.1 – 6.4.2, 6.5, 6.6.2 – 6.6.3, 6.7 • Virtual memory (if time permits): V&O 9.1 – 9.5 C: Advanced Topics

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

  5. Unit Contents • Pointers and Arrays • Structures • Input and Output C: Advanced Topics

  6. 1. Pointers and Arrays C: Advanced Topics

  7. Pointers and Addresses • A pointer is a variable that contains the address of a variable. • Pointers and arrays are very closely related. int x = 1, y = 2, z[10]; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ y = *ip; /* y is now 1 */ *ip = 0; /* x is now 0 */ ip = &z[1]; /* ip now points to z[1] */ • Declaration of a pointer variable: * • Reference operator: & • Indirection operator (oac dereference operator): * C: Advanced Topics

  8. int x = 1; int y = 2; int z[10]; int *ip; ip = &x; y = *ip; *ip = 0; ip = &z[1]; C: Advanced Topics

  9. int x = 1; int y = 2; int z[10]; int *ip; ip = &x; y = *ip; *ip = 0; ip = &z[1]; C: Advanced Topics

  10. int x = 1; int y = 2; int z[10]; int *ip; ip = &x; y = *ip; *ip = 0; ip = &z[1]; C: Advanced Topics

  11. int x = 1; int y = 2; int z[10]; int *ip; ip = &x; y = *ip; *ip = 0; ip = &z[1]; C: Advanced Topics

  12. int x = 1; int y = 2; int z[10]; int *ip; ip = &x; y = *ip; *ip = 0; ip = &z[1]; C: Advanced Topics

  13. Pointers and Function Arguments • How to write a function that swaps the values stored in two variables? swap(a, b); ... void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } • Is the above function correct? Why? At assembly time, this function call will be expanded to create three variables in the stack area. The stack pointer will be increased as the result. The return statement is hidden. At assembly time, the return statement will be expanded to delete three variables in the stack area. The stack pointer will be decreased as the result. C: Advanced Topics

  14. swap(a, b); ... void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } At this moment C: Advanced Topics

  15. swap(a, b); ... void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } At this moment C: Advanced Topics

  16. swap(a, b); ... void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } At this moment C: Advanced Topics

  17. How to write a function that swaps the values stored in two variables? swap(&a, &b); ... void swap(int *px, int* py) { int temp; temp = ???; ???; ??? = temp; } At this moment C: Advanced Topics

  18. swap(&a, &b); ... void swap(int *px, int* py) { int temp; temp = *px; *px = *py; *py = temp; } C: Advanced Topics

  19. swap(&a, &b); ... void swap(int *px, int* py) { int temp; temp = *px; *px = *py; *py = temp; } C: Advanced Topics

  20. swap(&a, &b); ... void swap(int *px, int* py) { int temp; temp = *px; *px = *py; *py = temp; } C: Advanced Topics

  21. swap(&a, &b); ... void swap(int *px, int* py) { int temp; temp = *px; *px = *py; *py = temp; } C: Advanced Topics

  22. int *test; int number = 20; test = &number; printf(“%d, %d, %p, %p\n”, number, *test, test, &test); // the content of the var pointed by test // the content of test *test = 30; printf(“%d, %d, %p, %p\n”, number, *test, test, &test); printf(“Enter an integer: ”); scanf(“%d”, &number); // the address of number printf(“%d, %d, %d, %p, %p, %p\n”, number, *test, *(&number), test, &test, &number); // the content pointed by test // the content pointed // by the address of number // the content of test // the address of test // the address of number C Programming

  23. Can you write a function to read multiple integers into an array? int data[10]; getint(data, 10); // read 10 integers into data[] ... ??? getint(???, ???) { ??? // using scanf() } C: Advanced Topics

  24. Pointers and Arrays • Strong relation between and pointers float *px; // px is ready to store an address. float x[10]; // x represents 10 float type variables, // x[0], x[1], ..., x[9], that are // alocated in cosecutive memory area. // x has the address of x[0]. float y; x[0] = 2; x[1] = 3; x[2] = 4; x[3] = 5; x[4] = 6; x[5] = 7; px = x; // the same data type? y = *px; printf(“%f, %f, %f\n”, x[0], *px, y); // ??? px = &x[2]; y = *(px+2); printf(“%f, %f, %f\n”, x[0], *px, y); // ??? C: Advanced Topics

  25. int number[10]; printf(“%p\n”, &(number[0])); printf(“%p\n”, &(number[5])); printf(“%p\n”, number); // related to reference printf(“%p\n”, number + 5); // the address of number[5] // not 5 * 4 newval(number); ... void newval(int num[]) { num[0] = 5; ... }, or void newval(int* num) { *num = 5; num[1] = 10; ... } • int num[] and int *num are equivalent. C Programming

  26. int number[10]; int *p, *q; *p = 10; // Is it wrong? p = number; q = &number[0]; number[0] = 2; number[1] = 9; number[2] = 5; printf(“%p, %p\n”, p, q); printf(“%d, %d, %d, %d\n”, *(p+1), p[1], *(q+1), q[1]); • Pointer variables and array variables can be used interchangeably. • Why do we need to use pointer variables? C Programming

  27. Do we really need to use pointers? • Dynamic memory management #include <stdlib.h> void *malloc(int size); // allocate size bytes // and return the addr void free(void *); // free the memory space ... int *p, n; scanf(“%d”, &n); p = (int*)malloc(sizeof(int) * n); p[0] = 10; *(p+1) = 20; C Programming

  28. Character Pointers #include <string.h.> gets(), puts() strcpy(), strlen(), strcmp(), strcat(), ... toupper(), ... C: Advanced Topics

  29. char name[256], tmp[256]; name[0] = ‘C’; name[1] = ‘O’; name[2] = ‘M’; name[3] = ‘P’; name[4] = ‘\0’; // it is very important. name[5] = ‘ ’; name[6] = ‘2’; name[7] = ‘1’; name[8] = ‘3’; name[9] = ‘0’; name[10] = ‘\0’; // it is very important. printf(“course number = %s\n”, name); printf(“%p\n”, name); printf(“course number = %s\n”, &(name[5])); scanf(“%s”, name); sprintf(tmp, “course name is %s.”, name); C Programming

  30. Pointer Arrays: Pointers to Pointers void f(int *x[13]); // 13 int* variables void f(int (*x)[13]); // pointer to an array of 13 ints // equivalent to int x[][13] • Command-line arguments int main(int argc, char *argv[]); • argv[0] the program name, e.g., a.out • argv[1] the first argument from the user C: Advanced Topics

  31. 2. Structures • User-defined data structure struct student_rcd { // class without methods in Java int student_number; char name[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; rp = (struct student_rcd *)malloc(sizeof(struct student_rcd), 3); rp->student_number = 20; rp[2].student_number = 30; ... C: Advanced Topics

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

  33. Self-Referential Structures struct tnode { /* the tree node: */ char *word; /* points to the text */ int count; /* number of occurrences */ struct tnode *left; /* left child */ struct tnode *right; /* right child */ struct tnode *parrent; }; struct tnode root; root.left = (struct tnode *)malloc(...); C: Advanced Topics

  34. Typedef typedef int Length; typedef char *String; 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; ... Length len, maxlen; Length *lengths[]; String p, lineptr[MAXLINES]; Treenode tnode; p = (String) malloc(100); C: Advanced Topics

  35. Unions union u_tag { // the shared storage int ival; float fval; char *sval; } ... union u_tag u; u.ival = 20; if (utype == INT) printf("%d\n", u.ival); if (utype == FLOAT) printf("%f\n", u.fval); if (utype == STRING) printf("%s\n", u.sval); else printf("bad type %d in utype\n", utype); C: Advanced Topics

  36. 3. Input and Output • Standard input from keyboard $ prog < infile input redirection $ otherprog | prog pipe • <stdio.h> • int getchar() • int putchar(char) C: Advanced Topics

  37. Formatted input • int scanf (char *format, arg1, arg2, ...) // from stdin • int sscanf (char *string, char *format, arg1, arg2, ...); // from string • The arguments must be references. C: Advanced Topics

  38. File Access #include <stdio.h> FILE *in, *out; in = fopen(“in_filename”, “r”); // mode: r, w, a, r+, w+, a+ if (in == NULL) ... out = fopen(“out_filename”, “w”); fclose(in); fprintf(out, “format ...”, variables...); fscanf(...); fgets(...); int fseek(FILE*, long, SEEK_SET or SEEK_CURRENT or SEEK_END); // move file position pointer int fwrite(void*, int memb_size, int no_memb, FILE*); int fread(void*, int memb_size, int no_memb, FILE*); C: Advanced Topics

  39. int fputc(int, FILE*); int fputcs(char*, FILE*); int fgetc(FILE*); int fscanf(FILE*, char* format, ...); int fprintf(FILE*, char* format, ...); Examples: • A file copy program, using fopen(), fseek(), fwrite(), fread(), fclose(). • Files containing student records C: Advanced Topics

  40. Error Handling – Stderr and Exit fprintf(stderr, char*, ...); exit(int); // non zero means error C: Advanced Topics

  41. math.h • Some MATH related functions • # include <math.h> • double sqrt(double); • double pow(double, double); • double fabs(double); • ... • Link with –lm-lm means libm.a, that contains math utilities, is used • $ gcc program3-5.c –lm C Programming

More Related