Week 10 Class 17 Tue Mar 16 Posted: Mar 16

Class session:

  1. Announcements:
  1. Screencast: Encapsulation & Information Hiding Part II notes
  2. Screencast: UML Class Relationships notes
  3. Exercise 44: UML Class Relationships Crossword
  4. Screencast: RAII notes
  5. Exercise 45: RAII Crossword
  6. Exercise 46: Memory.cpp I

    The file Memory.cpp in the repository Memory contains many of the memory resource problems described in the screencast.

    Dynamic memory in C++ is managed in the heap, which new and delete use. Misuse of new and delete can lead to memory management problems.

    The tool valgrind is a platform for dynamic analysis tools that detect multiple issues in a running program, including. The specific tool in valgrind for detecting memory management problems is memcheck.

    The following describes using valgrind with the online compiler CoCalc.com. You can also install valgrind into Ubuntu (sudo apt install valgrind) or install Docker and adjust the following instructions.

    1. Compile and run the program on your platform (i.e., your compiler and operating system). You may or may not see a memory-related issue during the build and run.
    2. Compile and run the program with each of the three compilers at the Online Compiler, rextester, g++, clang, and vc++. You should see some memory issues with at least one of the compilers.

    3. Go to CoCalc.com and create a C++ file with the contents of Memory.cpp. You can get a bash shell running by clicking on your file and using the >_Shell button. Compile, then run valgrind on the program:

      • CoCalc.com uses .c++ for a C++ file extension
      • The -g command for the compiler is for debug. It adds Memory.cpp line number information to the executable program Memory. This debug option on the compiler allows valgrind to refer to specific line numbers in your code.
      • The valgrind command has many complex options. Make sure to copy/paste the command.

      Valgrind produces an overwhelming amount of output. The important information is at the end. The interesting parts of the output are the HEAP SUMMARY:, LEAK SUMMARY, and ERROR SUMMARY. An unsuccessful run with memory leaks of Valgrind ends with something like:

      A successful run with no memory leaks of Valgrind ends with something like:

    4. Fix the memory resource problems in Memory.cpp without using std::unique_ptr. Get it to the point where it compiles runs successfully on all compilers, and you have a clean valgrind run.

    Commit your changes. Tag this in Git as fixmemory

    The Memory repository is available via GitHub Classroom with a link on the Brightspace course page.

  7. Exercise 47: Memory.cpp II

    Replace the use of raw pointers (e.g., int* p) with std::unique_ptr<int>. Make sure it runs successfully on all compilers, and you have a clean valgrind run. The include file for std::unique_ptr<> is <memory>.

    Commit your changes. Tag this in Git as smartpointer

Screencast Folder: Class 17 Tue Mar 16

Exercise Due Date: 4 pm on Wednesday, Mar 17

Reminders: