Thursday, May 10, 2012

11.24

In the algebraic specification of a stack or queue, no mention was made of the possibility that a stack or queue might become full:

full: stack -> boolean

Such a test requires the inclusion of another operation, namely,
size: stack -> integer
and a constant
maxsize: integer

Write a set of axioms for such a stack.

fullstack(createstack) = error
sizeofstack(fullstack) = x
sizeofstack(maxsize) = x

Wednesday, May 2, 2012

10.27

Suppose that we wanted to state a rule that would make the C dangling reference created by the following function illegal:


int* dangle(void)
{
     int x;
     return &x;
}


Suppose that we decided to make the following statement: the address of a local variable cannot be a returned value and cannot be assigned to a nonlocal variable. 
(A) Can this rule be checked statically?
Yes


(B) Does this rule solve the problem of dangling references created by the use of a stack based environment?
Yes

10.17

Draw the stack of activation records for the following C program after (a) the call to r on line 13 and (b) after the call to p on line 14. Show the control links, and fill in the local names in each activation record. (c) Describe how variables r and x are found on lines 4 and 5 during the execution of p




  1. int x;
  2. void p(void)
  3. {
  4.      double r = 2;
  5.      printf("%g\n",r);
  6.      printf("%d\n",x);
  7. }
  8. void r(void)
  9. {
  10.      x = 1;
  11.      p();
  12. }
  13. void q(void)
  14. {
  15.      double x = 3;
  16.      r();
  17.      p();
  18. }
  19. main()
  20. {
  21.      p();
  22.      q()
  23.      return 0;
  24. }
Main
p
q
r
Free Space

Main
p
q
r
Free Space

10.8

Give the output of the following program written in C syntax using the four parameter passing methods discussed in section 10.3


int i
int a[2];


void p(int x, int y)
{   x++;
     i++;
     y++;
}
main()
{   a[0] = 1;
     a[1] = 1;
     i = 0;
     p(a[i],a[i]);
     printf("%d\n",a[0]);
     printf("%d\n",a[1]);
     return 0;
}


It prints:
1
1


it doesn't increment the values as one might think because they're passed by value, not by reference

10.6

In C,


void f(int x, double y);
void f(int a, double);
void f(int, double z);
void f(int y, double q)
{ if (q> y) printf("ok!\n");
   else printf("not ok!\n");
}


would it be legal to include all of the declarations in the same code file? Explain.


No, it would not, straight C doesn't allow function overloading.  It wouldn't even be allowed in C++ because the function declarations are all of the same form( identical type, name, and order and number of variable types).

Tuesday, May 1, 2012

8.23

In C++ the equality test == can be applied to arrays, but it tests the wrong thing. Also, the assignment operator = cannot be applied to arrays at all. Explain


The name of an array references the memory address of the first element of the array, so using == does not test if the arrays themselves are equal, but rather whether they exist at the same memory location. The assignment operator =, when applied to an array, would result in two arrays existing in the same location, and so the compiler prevents this.