An implicit contract with the free store manager
On this page
An implicit contract
Clause 1 and memory leaks
Clause 2 and dangling pointers
Clause 3 and runtime errors
Questions
Q1
QFree Store Contract
This question is ungraded.
Identify which type of violation of contract with the free store manager is present in the following snippet of code:
void Foo(int*& ptr) {
delete ptr;
ptr = nullptr;
}
int main() {
int* pt = new int(11);
int* al = pt;
Foo(pt);
std::cout << *al << std::endl;
}
Q2
QFree Store Contract Q2
This question is ungraded.
Identify which violation of the free store contract presents in the following snippet of code:
void Foo(int*& ip) {
ip = new int;
}
int main() {
int* pt = new int(7);
Foo(pt);
delete pt;
}
Q3
QFree Store Contract Q3
This question is ungraded.
Consider a situation where we'd like to pass a pointer to a function, have the pointer point to something else, and have that assignment reflected in the caller. To accomplish just that, we can pass a pointer by reference to a function! Given the following declaration:
void MyFunction(int*& param);
what is the type of MyFunction's parameter param?
Hint: the declarator operators in the declaration int*& param bind to the type with
left-to-right associativity. Expanding out from the type, we see int followed by *.
At this point, we have an int* aka a pointer to an int object.
Subsequently, we observe the &, (int*)&. Therefore, we can read
this declaration from right-to-left: param is a reference to a pointer to an
integer object.
Q4
QFree Store Contract Q4
This question is ungraded.
With respect to the following code snippet,
void Foo(int*& ip) {
/* do something */
}
int main() {
int* pt = new int(7);
Foo(pt);
}
which of the following statement is correct?