COMP 1917 Computing 1
Session 2, 2016

Tutorial - Week 7


Presentation Topic for This Week

Explain what is meant by a "Buffer-Overflow Attack". What kind of code is susceptible to such an attack? How can you re-write your code to protect it from this threat?


  1. Write a program that scans in the size of an array and then uses the function int *readArray(int size) to dynamically allocate an array of appropriate size and populate it with numbers from standard input.
    int main(int argc, char *argv[]){
        int size = 0;
        scanf("%d", &size);
    
        return 0;
    }
    
    int *readArray(int size){
    
    }
    
  2. How would you free the allocated block after you've finished using it? What are some of the implications of not freeing the memory?
  3. What is wrong with the following example

    int main(int argc, char *argv[]){
        int size = 0;
        scanf("%d", &size);
        int *arr = createArray(size);
    
        return 0;
    }
    
    int *createArray(int size){
        int new_array[size];
        return new_array;
    }
    
  4. A point in two dimensional space can be stored as an array with two entries, or as a structure with two components x and y:
        typedef struct point Point;
    
        struct point {
          double x;
          double y;
        };
    
    For each of these two representations, write a function which, given two points p and q, returns the distance between them:
        double distA( double p[2], double q[2] );
    
        double distS( Point p, Point q );
    
    Do you find one version of the function "easier to read" than the other?

  5. Write a program that uses struct _student with attributes int zid and double height. Create an array of these structs and populate each struct with infromation from standard input.

    If time allows:

  6. Extend previous program to use function tallest_student that takes in an array of student structs and returns zid of the tallest student.

  7. Complete the following program that uses the function allocate_string to dynamically allocate read in strings.
    char * allocate_string(char * current);
    
    int main(int argc, char *argv[]){
    
        char buffer[100];
        char *strings[100];
        int count = 0;
    
        while(fgets(buffer,100,stdin)!= NULL && count < 100){
    
            strings[count] = allocate_string(buffer);
            count++;
        }
        int i;
        for(i = 0; i < count; i++){
            printf("%s\n",strings[i]);
            free(strings[i]); 
        }   
    
        return 0;
    }
    
    
    char * allocate_string(char *current){
    
    }
    
    What is the benefit of dynamically allocating strings over having a static array char strings[100][100]?

Presentation Topic for Week 8

Discuss how and why abstraction is used in computer science? Can you think of an example of a function you used in this course where the internal complexity was abstracted away?