Monday, April 16, 2012

Chapter 7

7.1
7.8
7.14
7.26
7.31
7.32
7.33


7.1


Binding Times
Language definition time
Language implementation time
translation time ("compile-time")
Link time
Load time
Execution Time("runtime")


a) The maximum number of significant digits in a real number would fall under the Language implementation binding time, because in C and Java there is a maximum number of digits that a number type can represent even before you have written a program, so that must have been setup at the time the language implementation was, because even if there was a number during the definition time, it would change during testing and tweaking of the implementation.


b) The meaning of char would have language definition binding time, because they couldn't have implemented a meaningful implementation without first having a definition of what char meant. 


c) The size of an array variable would have translation or compile-time binding time because in C or Java the size of an array is defined by the programmer while coding the program. So logically the next step is translation time, or possibly link time if the array was an external binding that needed to be brought in.


d) The size of an array parameter would either be during translation time or link time, because it could be referenced by an external object or it could be external itself and the would need to be linked before any parameters are passed to it. 


e) The location of a local variable would have translation binding time because the variables would need to be stored to be useful in the other phases.


f) The value of a constant would also have translation binding time because first it's coded then it must be compiled for it to be significant, and that's when it's stored too.


g) The location of a function would also be during translation binding time because that's when most languages do storage allocation.


7.8
name                   binding

Point 1
a                        int(global)      int(local to p)         
b                        int(global)     
p                        function(int)
p                        int(local to p) 

Point 2
a                        int 0(global)   int 3 (global)        
b                        int 1(global)   int 4 (local to q) 
p                        int 2(global)  
print                   function(void)
q                       function(void)

Point 3
a                       int p()(global)    int 2(local to p)

7.14

The reason that the first p()  outputs garbage values is because you have only allocated the memory for int y and then you print it before assigning a value to it. Since you have assigned a block of memory for this variable, you don't know what was in that memory before, and since you haven't over-written that memory you get what was in there before. So the result of the first p() is garbage. The second call to p() produces 2 because you set it in the first p() after you printed it's value.


7.26

Given the following C program, draw box-and-circle diagrams of the variables 
after each of the two assignments to **x (lines 11 and 15). Which variables 
are aliases of each other at each of these points? What does the program print? 


Code: http://pastie.org/3801535 

Line 11 is **x = z
Line 15 is **x = 4



So at this line, z, *y, and **x are aliases since z isn't a pointer and has a value, and in the end z *y, and **x both point to the value '1'. 




After line 15, we have the following aliases.The value of the second value has changed from z to 4, which '4' and not '3' which is the new value of z. So the only aliases now are **x and *y.


This program would print:


1
1
3


At first glance, this program should print:


1
3
3


The reason that the value of y doesn't change after z is changed to 3, is the fact that it wasn't pointing to z's address, but it's value.

7.31

Why is the following C code illegal?

{
   int x;
   &x = (int *) malloc(sizeof(int));
   ...
}

The reason that the following is illegal is due to the fact that you can't cast a pointer reference (address) into the value, nor can you allocate memory for "the reference", but you allocate memory for the value, and the reference is where it is stored.


7.32


Why is the following C code illegal?



{
   int x[3];
   x = (int *) malloc(3*sizeof(int));
   ...
}


The following is illegal due to the fact that x is not a pointer, and that arrays have "automatic" memory allocation. That means when you entered the amount of elements in that array, C already knows how much memory to assign. Although, if you had *x and you wanted to create an array, the previous allocation would be perfect if you needed 3 elements.


7.33



#include <stdio.h>

int gcd(int u, int v){
    if (v == 0){
        return u;
    } else {
        return gcd(v, u % v);
    }
}

int (*fun_var)(int,int) = &gcd;

main(){
    printf("%d\n", (*fun_var)(15,10));
    return 0;
}



The previous code (http://pastie.org/3822151) is legal because you are creating a variable that points to a function. This variable takes two parameters, because that's what the function requires. This is legal because it's only a memory reference to the gcd function. Since *fun_var is of type int and has two int parameters, it's a valid function to take on the reference of gcd. It can then be called instead of the gcd function (as we see in the printf arguments).



No comments:

Post a Comment