Skip to content

Latest commit

 

History

History
155 lines (114 loc) · 4.93 KB

README.md

File metadata and controls

155 lines (114 loc) · 4.93 KB

Chapter 12. Dynamic Memory

Exercise 12.1:

How many elements do b1 and b2 have at the end of this code?

StrBlob b1;
{
    StrBlob b2 = {"a", "an", "the"};
    b1 = b2;
    b2.push_back("about");
}

b2 is destroyed, but the elements in b2 must not be destroyed.

so b1 and b2 both have 4 elements.

Exercise 12.2

StrBlob | TEST

Exercise 12.3:

Does this class need const versions of push_back and pop_back? If so, add them. If not, why aren’t they needed?

You can certainly do this if you want to, but there doesn't seem to be any logical reason. The compiler doesn't complain because this doesn't modify data (which is a pointer) but rather the thing data points to, which is perfectly legal to do with a const pointer. by David Schwartz.


Discussion over this exercise on Stack Overflow

Discussion over this exercise more on douban(chinese)

Exercise 12.4:

In our check function we didn’t check whether i was greater than zero. Why is it okay to omit that check?

Because the type of i is std::vector<std::string>::size_type which is an unsigned.When any argument less than 0 is passed in, it will convert to a number greater than 0. In short std::vector<std::string>::size_type will ensure it is a positive number or 0.

Exercise 12.5:

We did not make the constructor that takes an initializer_list explicit (7.5.4, p. 296). Discuss the pros and cons of this design choice.

@Mooophy:

keyword explicit prevents automatic conversion from an initializer_list to StrBlob. This design choice would easy to use but hard to debug.

@pezy:

Pros

  • The compiler will not use this constructor in an automatic conversion.
  • We can realize clearly which class we have used.

Cons

  • We always uses the constructor to construct a temporary StrBlob object.
  • cannot use the copy form of initialization with an explicit constructor. not easy to use.

Exercise 12.8:

Explain what if anything is wrong with the following function.

bool b() {
    int* p = new int;
    // ...
    return p;
}

The p will convert to a bool ,which means that the dynamic memory allocated has no chance to be freed. As a result, memory leakage will occur.

Exercise 12.9:

Explain what happens in the following code:

int *q = new int(42), *r = new int(100);
r = q;
auto q2 = make_shared<int>(42), r2 = make_shared<int>(100);
r2 = q2;
  • to q and r:

Memory leakage happens. Because after r = q was executed, no pointer points to the int r had pointed to. It implies that no chance to free the memory for it.

  • to q2 and r2:

Exercise 12.19 Header|Implementation

Exercise 12.21:

We could have written StrBlobPtr’s deref member as follows:

std::string& deref() const
{ return (*check(curr, "dereference past end"))[curr]; }

Which version do you think is better and why?

the origin version is better. cause it's more readability and easier to debug.

Exercise 12.22 Header|Implementation

Exercise 12.25:

Given the following new expression, how would you delete pa?

int *pa = new int[10];
delete [] pa;

Exercise 12.27 Header|Implementation|Test

Exercise 12.29:

We could have written the loop to manage the interaction with the user as a do while (5.4.4, p. 189) loop. Rewrite the loop to use a do while. Explain which version you prefer and why.

do {
    std::cout << "enter word to look for, or q to quit: ";
    string s;
    if (!(std::cin >> s) || s == "q") break;
    print(std::cout, tq.query(s)) << std::endl;
} while ( true );

I prefer the do while, cause the process according with our logic.

Exercise 12.30 Header|Implementation|Test

Exercise 12.31:

What difference(s) would it make if we used a vector instead of a set to hold the line numbers? Which approach is better? Why?

The vector can not ensure no duplicates. Hence, in terms of this programme set is a better option.

Exercise 12.32 Header|Implementation

Exercise 12.33 Header|Implementation