int *g;
// make the location pointed to by g contain the same value as p
void f(int *p) {
g = p;
}
int main() {
int *x = new int;
*x = 5;
f(x);
delete (x);
printf("g is: %d\n", *g);
}
- [skill 8.1] Does the above program suffer from memory leak or dangling pointer bugs? If so, indicate where and why.
- [skill 8.2] Rewrite the program above to eliminate the memory leak or dangling pointer bugs.
Let's consider the following abstract call F(A1, ... An). F is the procedure being called. While we are used to calling a procedures directly (e.g., print(...)), most languages, including MYSTERY allow complicated expressions for F. For example, if a is an array of procedures, one can do a[2](5) in MYSTERY to call the procedure at a[2] with argument 5. MYSTERY even allows a procedure to return a procedure. Thus, if a procedure, f, returns a procedure, then one could write f()(x). This calls the procedure returned by f with the argument x. Needless to say, most languages allow one to pass arbitrarily complicated arguments (i.e., Ai).
- Come up with and describe in detail a compelling example of why would one want to return a procedure from a procedure.
- Discover whether MYSTERY evaluates the procedure being called before it evaluates its arguments. For example, with f()(x) does MYSTERY call the function f first or does it evaluate x first (i.e., looks up the value of x). You may execute up to 4 PRINT statements.
- Discover whether or not MYSTERY evaluates arguments to a call from left-to-right. You may execute up to 4 PRINT statements.
Use this link to submit programs. You should provide evidence that supports your argument. You will lose 5% of the points for this question for each additional executed PRINT.