Question 2#
Which of the following statements about the program below are true?
Statement |
True |
|---|---|
The output of the following program run is empty, and the exit status is 0. $ ./example2 0
$ echo $?
0
|
Statement |
True |
|---|---|
Output and exit status are: $ ./example2 1
Foo()
bar()
~Foo()
$ echo $?
0
|
Statement |
True |
|---|---|
Output and exit status are: $ ./example2
need one parameter
$ echo $?
0
|
Statement |
True |
|---|---|
The program has a memory leak |
Statement |
True |
|---|---|
The program’s behavior is undefined |
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)
f = new Foo;
if (i)
f->bar();
if (i)
delete f;
return 0;
}