10.6
10.8
10.17
10.27
10.2 Increment Procedure in C
void inc(int* x){ *x++; }Is this legal? If so, what does this procedure do? If not, why?
This is legal, but what it produces isn't exactly helpful. If we pass int *x = 5; to this procedure we get back the same 5 which we put in. This is how I tested the procedure.
int *x = 5; printf("\n%d", x); inc(x); printf("\n%d", x);
Furthermore, if you initialize a pointer without a value, and then set the value to an integer, we get a Segmentation fault. This is a very vague pointer error which occurs during execution and isn't caught during compile.
10.6 Multiple function declarations; same file.
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"); } }These declarations are perfectly legal because they don't even require symbols for the variables so if you have multiple declarations/prototypes with different symbols the definition will use the symbols of it's formal parameters. Just make sure that the order of the parameter types is the same (you'll get conflicting types if you don't), or don't do that at all...
ps, prints "ok!" if f(1,2.0) is evaluated.
10.8 4 parameter-passing methods
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; }Using the four parameter-passing methods (output):
- Pass-by-value: 1 1
- Pass-by-reference: 1 1
- Pass-by-value-result: (can't find example on how to do this in c)
- Pass-by-name: 1 1 (I think, once again no example..)
a) The following is the activation record when r() is called on line 13 from q():
b) After the call to p() on line 14:
c) On lines 4 and 5 in p() x and r are attained by different means. r is a local variable, so it is attained locally. X is a different story, and can be attained in three different ways, both nonlocal. First of all, if p() is called from main x is taken from the global x and prints garbage since the global isn't assigned. If p() is called from q() we get the value 3 and a double not int. Furthermore, if r() is called from q() it also calls p() which would get the value 1 as a double from q().
10.27 Dangling references
int* { int x; return &x; }
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, it can also be checked dynamically. Although there are certain situations when it cannot be (not sure what they are though). The scope of the variable can be checked if being assigned an address.
b) This rule would solve the problem of dangling references, since there would be no way to create a dangling reference if the variables that take on the address are local and their activation record is deleted when the function terminates. So if the address and the function using that address are terminated at the same time, then there is no problem of dangling references.
No comments:
Post a Comment