Skip to content

Latest commit

 

History

History

Folders and files

README.md

Chapter 1: Programming

Lecture notes by: Michael Hahsler

Diagrams for Chapter 1

This chapter is a review of important concepts and their C++ implementation used in this course. Very good short explanations can be found in this C++ YouTube Playlist.

Classes and Compiling

Dynamic Memory Allocation and Pointers

  • Some Questions:

    • Where do programs store variables and objects?
    • Where does memory come from and how is memory returned?
    • What is dynamic memory and why do we need dynamic memory allocation?
    • What is the difference between the stack and the heap.
    • What happens when you access memory that is not yours?

    See program memory, segmentation fault.

  • Example: Create a memory diagram for executing the following program:

    int inc(int x) return x + 1;
    
    int main() {
      int a = 10;
      int b = inc(a);
      return 0;
    }
  • Video: How do create memory diagrams

  • Pointers and dynamic memory allocation

  • Call-by-value vs call-by-reference and using const.

  • Reading: References vs. pointers

  • Exercise: Memory allocation

  • What are the differences between memory management in C, C++, Java and Python? (Most programming languages use very similar mechanisms.)

    C C++ Java Python
    Request dynamic memory ? ? ? ?
    Return dynamic memory ? ? ? ?
  • Destructor, copy constructor, assignment operator, and move semantics: The rule of three and the big five for classes.

Templates (a.k.a. Generics)

Standard Template Library

Most programming languages come with a library of ready-to-use data structures (abstract data types often called collections) and algorithms to supplement its primitive data types. Java has the Java collections framework including [java.util]. Python has a standard library built into the language.

C++ has the C++ Standard Template Library (STL).

File IO

Command Line Interface

Recursion

Testing

All code needs tests! Ways of testing are described in HOWTO Test.

Example: Testing a function to calculate factorials.

License

CC BY-SA 4.0

All code and documents in this repository are provided under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) License.