Question 3#
Which of the following statements about the program below are true?
Statement |
True |
|---|---|
The following program run is ok $ ./example3 0
|
Statement |
True |
|---|---|
The following program run exhibits undefined behavior $ ./example3 1
|
Note: std::stoi() converts its parameter to int, or raises
an exception if that is not convertible (e.g. "xxx")
#include <iostream>
#include <string>
class Foo
{
public:
Foo() {
std::cout << "Foo()" << std::endl;
}
~Foo() {
std::cout << "~Foo()" << std::endl;
}
void bar() const {
std::cout << "bar()" << std::endl;
}
};
int main(int argc, char** argv)
{
Foo* f = nullptr;
if (argc != 2) {
std::cerr << "need one parameter" << std::endl;
return 1;
}
int i = std::stoi(argv[1]);
if (i) {
Foo some_f;
f = &some_f;
}
if (i)
delete f;
return 0;
}