CS010 Practice 5

Debuggers and C Memory Management

 
  1. Look at problem 5 from Practice 2 again. Use ddd or gdb to help you figure out exactly what is happening.
  2. Look at problem 3 from Practice 4 again. Use ddd or gdb to help you figure out exactly what is happening.
  3. What does the following code do? Reason it through and then compile it and see what happens. You will get a warning. Do you understand what it means? The compiler creates an executable file even though there is a warning. Try running this code. What happens? Fix the code so that both sets of print statements print the same thing.
    typedef struct {
        int month;
        int day;
        int year;
    } date;
       
    typedef date * dateptr;
       
    dateptr nextDay (date d);
    void printDates (date d1, dateptr d2);
       
    int main () {
      date today;
      dateptr tomorrow;
       
      today.month = 1;
      today.day = 10;
      today.year = 2000;
       
      tomorrow = nextDay (today);
      printf ("Date 1 is %d/%d/%d\n", today.month, today.day, today.year);
      printf ("Date 2 is %d/%d/%d\n", tomorrow->month, tomorrow->day, tomorrow->year);
       
      printDates (today, tomorrow);
      exit (0);
    }
       
    dateptr nextDay (date d) {
      date d2;
       
      d2 = d;
      d2.day = d2.day + 1;
      return &d2;
    }
       
    void printDates (date d1, dateptr d2) {
      printf ("Date 1 is %d/%d/%d\n", d1.month, d1.day, d1.year);
      printf ("Date 2 is %d/%d/%d\n", d2->month, d2->day, d2->year);
    }