Termipankki
  1. A
    1. API
    2. ASAN
    3. Access specifiers
  2. C
    1. Constructor
    2. const
  3. G
    1. GDB
    2. g++
  4. M
    1. Make
    2. Memory Leak
  5. P
    1. Pointer
  6. R
    1. Recursion
  7. S
    1. std
  8. V
    1. Valgrind
Ratkaistu: / tehtävää

Setting Up Your Work Environment

Linux Subsystem Installation

Unless you already have Linux installed on your machine, we recommend you install Windows Subsystem for Linux (WSL). This will allow you to run a Linux-based operating system natively within Windows, and to use Linux tools and utilities directly on your Windows machine, without needing to set up a virtual machine or dual boot the two operating systems.
To install WSL on Windows 11 follow the steps below:
1. Right-click on the Start button in the lower-left corner of your screen, and from the menu that appears, select either PowerShell (Admin), Terminal (Admin), or Command Prompt (Admin) to open the respective program with administrator privileges.
2. In case you want to avoid modifying your Windows settings and enabling Virtual Machine Systems, you may opt for installing WSL1 instead of WSL2. To do so, type the following command:
wsl --set-default-version 1 
3. Now you can start installing WSL by typing:
wsl --install
4. Restart the computer.
5. Find the newly installed Ubuntu in the Windows menu by clicking on the Start button in the lower-left corner of your screen, and scrolling through the list of installed applications. Once you locate it, click on it to launch the Ubuntu terminal and start using the Linux environment on your Windows machine.
6. Follow the prompts to install Ubuntu.
7. Select your username and password.

C++ Tools

You're almost ready to start using the Linux Subsystem. The only thing left is to set up C++:
sudo apt update
sudo apt install build-essential
This will update your system's package list and install the necessary tools for C++ compilation and execution.

Using WLS

If you have experience working with Linux environments, you may skip the following instructions. However, if you are new to Linux, the following guidelines will be helpful in getting you started.
To open the Windows Explorer of your Linux Subsystem home directory, enter the following command in the terminal:
explorer.exe .
This will launch a window displaying the contents of your home/username directory, which you can use to create directories and files for your assignments.

Linux Command Line

Using WSL Ubuntu command line is essentially the same as native Ubuntu. However, running GUI programs require some extra work.
Instead, WSL has the advantage that you can use Windows programs for your files freely, such as text and image editors.
For compiling and running the programs for this course, the command line is ideal.
Useful Linux commands:
pwd # Print Working Directory - shows the folder you are currently in

ls # List - lists the files in your current folder

cd FOLDER # Change Directory to FOLDER - opens FOLDER
cd .. # - opens the parent folder of your current location
cd # - opens your home folder

vim FILE # Opens (or creates) FILE in vim, a simple command line text editor

rm FILE # Remove - deletes FILE
rm -r FOLDER # - deletes a folder and its contents
mv FILE1 FILE2 # Move - moves/renames FILE1 to FILE2
cp FILE1 FILE2 # Copy - copies FILE1 to FILE2

make PROGRAM # Compiles PROGRAM using the rules in the Makefile file in current directory

clear # Clear the terminal screen (Ctrl-l also works)
reset # Reset and clear the terminal
Keyboard shortcuts for Bash:
Ctrl + a : Navigates to the beginning of the line
Ctrl + e : Navigates to the end of the line
Alt + b : Back (left) one word
Alt + f : Forward (right) one word
Ctrl + u : Clears the line from the cursor to the beginning
Ctrl + c : Kills the current command being executed (useful if run into an infinite loop)
tab : Attempts to autocomplete your command 

Text Editor

We do not require you to use any specific text editor. It can be a text editor within WSL Ubuntu or a text editor in Windows or whatever you like.
Some examples:
It is not recommended to set up a fully fledged IDE as that can result in getting different kinds of warnings and suggestions that are not in line with the course material, and you may miss out on learning important aspects of C++ development or even arrive at incorrect solutions if you follow the guidance of the IDE more closely than the course material.
If you are using WSL and you are unsure which text editor to pick, the easiest way to get started is likely Notepad++.

Common problems

1. If you download files on Windows and move them to WSL directory, in some cases you don't have appropriate permissions for those files in WSL. In that case, use the chmod command:
sudo chmod 777 -R [FILE/FOLDER]
This command will ensure you have all the permissions for that file or all the files under a folder.
2. If you extract or otherwise add files to a WSL directory and you have the directory open in Windows explorer, you probably won't see the new files immediately, but rather you have to refresh the folder by pressing F5 or perhaps just re-opening the folder.
?
API stands for Application Programming Interface. In the context of this course, you can consider the header files to be such interfaces, as they determine what class functions and properties are public and thus accessible from anywhere.
AddressSanitizer (ASAN) is a memory error detector for C/C++. In this course, the makefiles will typically compile an executable that uses ASAN, with "-asan" at the end of its name.
The two notable access specifiers are:
  • public: class members defined after the public keyword are accessible from outside the class.
  • private: class members are generally private by default and thus not accessible from the outside
Constructor is a special non-static member function of a class that is used to initialize objects of its class type. A constructor is called upon initialization of an object. A constructor without arguments is a default constructor, but constructors that take arguments can be defined.
GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed.
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
  • Start your program, specifying anything that might affect its behavior.
  • Make your program stop on specified conditions.
  • Examine what has happened, when your program has stopped.
  • Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
GNU Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files. Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files. When you write a program, you should write a makefile for it, so that it is possible to use Make to build and install the program.
Memory leak means that the program is not freeing up memory it reserves. The memory will be freed when the program terminates, but if a program keeps leaking more and more memory without terminating, it can become a huge issue!
A typical way for memory leaks to occur is reserving memory with new and not calling delete before the pointer goes out of scope.
Pointer variables store a memory address as their value. In other words, they point to some data. The data can be accessed by dereferencing the pointer. (Either like *p or p->...)
A recursive function calls itself from within it. The recursion call must be conditional or it would lead to an infinite loop.
Valgrind is another tool besides ASAN that you can use in this course. It can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behaviour.
const is a keyword meant to mark something as immutable
  • A const object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.
  • const keyword on an object's member function prevents the object from being modified in the function
  • Pointer declarations can have 2 const keywords, one to make the data that's pointed at unable to be modified and one to make the pointer itself unable to be modified
Using const improves code readability and prevents accidental modification of objects.
g++ is a C++ compiler that we primarily use for this course. It is the C++ compiler for the GNU compiler collection. You may sometimes see gcc being used instead of g++, which was originally the GNU C compiler, but calling gcc generally also compiles .cpp files as C++. However, calling g++ is preferred.
In C++, std stands for Standard Library, which is a collection of commonly useful classes and functions. Typically, these are defined in the std namespace.