ece449-hwk04-sol.pdf - Homework 04 Solutions ECE 449 Fall...

This preview shows page 1 - 3 out of 3 pages.

Homework 04 Solutions ECE 449, Fall 2017 1. (4 points) Review the following pieces of code and briefly explain potential issues. A. std::string &get_hello() { std::string s = "hello"; return s; }
B. void some_function(T *a); void test() { T *p = new T; some_function(p); delete p; }
C. class obj_vec { std::vector<T *> vec_; public: obj_vec(size_t n) { for (size_t i = 0; i < n; ++i) vec_.push_back(new T); } ~obj_vec() { for (size_t i = 0; i < vec_.size(); ++i) delete vec_[i]; } };
1
ECE 449 – Object-Oriented Programming and Computer Simulation, Fall 2017 2
Answer: There is undefined behavior since tokens_ is destroyed twice, once

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture