CS140: Homework 7

  1. You are given the following declarations:
    char job[30] = "Hello World";
    char *dest;
    Jval j;
    IS input_file = new_inputstruct(0);
    Use a checkmark to indicate which of the following statements will be flagged as a warning or error by the compiler and which statements may cause the program to arrive at a wrong result or to core dump. Consider each set of statements separately and do not assume that any variable other than input file or job has been initialized (by the way job is really a 30 character array even though it is initialized to a smaller character string). If you think that a statement will be flagged by the compiler explain why. If you think a set of statements will compile correctly but will cause a problem during execution try to rewrite them. It may be possible to either correct the problem or to write a conditional that ensures the statement(s) is executed only if a certain pre-condition is met.

    1. strcpy(job, "brad"); dest = job;
    2. job = (char *)malloc(sizeof(char) * 50);
    3. if (get_line(is) >= 0 && is->NF >= 2) { dest = is->fields[1]; }
    4. if (get_line(is) >= 0 && is->NF >= 2) { strcpy(job, is->fields[1]); }
    5. strcpy(dest, job);
    6. strcpy(j.s, job);
    7. j.s = strdup(job); return j.s;
    8. dest = strdup("brad"); job = dest;
    9. dest = (char *)malloc(sizeof(char) * 10); strcpy(dest, "bradley"); j.s = dest;
    10. For the following set of statements assume that the string being read is less than 30 characters:

      scanf("%s", job); dest = job; return dest;

  2. How many bytes will be allocated to the following union?
    union Item {
        struct {
    	int grades[5];
    	char name[20];
    	char sex;
        } student;
        struct {
    	Dllist courses;
    	char name[20];
    	char office[10];
    	int job_type;
    	char *address;
        } professor;
        int ssn;

  3. How many bytes will be allocated to the following struct?
    struct account {
        int type;
        char name[30];
        double balance;
        union {
    	struct {
    	    int check_num;
    	    Dllist checks;
    	} checking;
    	struct {
    	    double interest_rate;
    	    double quarterly_interest;
    	    double annual_interest;
    	} savings;
        } acct_info;
    Assume that a double requires 8 bytes of storage.

  4. Rewrite Dr. Plank's new_stack, stack_push, and stack_pop functions so that they use Dllists. The functions should take the same parameters and return the same types but should use his Dllist library rather than his linked data structure. You will have to also re-write Dr. Plank's stack struct to use a dllist. Do not worry about compiling or executing your code.

  5. For each of the following problems indicate whether you would use a singly-linked list, doubly-linked list, or array.

  6. Write a short program that simulates pushing and popping functions from a stack. Your input consists of lines from standard input that begin with either the keyword "call" or "return". If a line starts with "call" it also has a second field which is the name of a function. If a line starts with return it has an optional second field which is the name of a function. You are to write a program that uses the fields library to read these lines. Your program should use Dr. Plank's stack library and it should perform the following actions:

    The following example shows some sample input and what the stack would look like after each line is processed. Of course the function names would be pointed to by character strings and the actual values on the stack would be Jvals that pointed to the character strings.

    	call a          // stack = a
    	call b		// stack = b a
    	call a		// stack = a b a  (a was pushed onto the front)
    	return		// stack = b a    (b was popped)
    	call c		// stack = c b a
    	call d		// stack = d c b a
    	call a		// stack = a d c b a
    	return c	// stack = c b a (a and d got popped)
    Do not worry about printing anything, checking for error conditions, or compiling/executing your code.