Basics of C/C++ Programming
Introduction
Lecturers
Rait Liiv: rait.liiv@itcollege.ee
Janno Tomingas: janno.tomingas@itcollege.ee
C
Lectures
Practices
Practice 1
Console input/output, conditions, loops, pointers, arrays, functions
Practice 2
File input/output, dynamic memory allocation, structs, .h/.c files
Additional info:
Always close the file stream after you are done using it.
Make sure to check for error codes.
2. Write a program that dynamically allocates a small array of ints. Fill the array and print its contents. Free the memory after you're done using it.
Additional info:
Use "fseek", "ftell" and "rewind" to find out how large the buffer should be.
Make sure you have enough room for a null terminator. (You may have to assign it to the end of the array yourself)
Remember to free the allocated memory after calling malloc or calloc.
Make sure to check for error codes.
4. Create an implementation of a stack data structure (Stack (abstract data type)) that uses a dynamically allocated array for storage.
- Required functions:
- init - Initializes stack's variables and allocates required dynamic memory with an initial(default) size,
- destroy - Frees all allocated memory and optionally resets other stored information for a stack,
- push - Adds an element to the top of the stack,
- peek - Gets the top element of the stack,
- pop - Gets and removes the top element of the stack;
- Describe the stack struct and functions in a separate .h file,
- Implement the functions in a corresponding .c file,
- Test your stack with some operations. Remember to check for edge cases;
- How do you know which element to access?
- How do you know if an array is full?
- How should the functions behave when a stack is empty?
- How should the functions behave when a stack is full?
- What will happen if any of the functions are called many times or in unexpected order?
5. Write a program that reads an unknown number of integers (each on a separate line) from a file into the stack you created. After reading the whole file, print out all ints that were read (in reverse order).
Assignment
Write a program that reads values from a file and outputs the result to stdout.
Input
Input file's name is "input.txt".
The file contains index-value pairs of integers on a single line. Integers in a pair are separated with a comma (,) and pairs are separated with a semicolon (;).
All indices between 0 and (count_of_pairs - 1) are guaranteed to be present.
The indices are guaranteed to be unique.
The indices are not guaranteed to appear in an ascending order.
Sample input: 1,4;0,2;2,8
Output
The program should output the differences between consecutive values as the index increases.
Output should be printed as a comma separated list of values on a single line to stdout.
For input:
0,1;2,4;1,2
Calculates:
0,1 to 1,2 -> 2 - 1 = 1 1,2 to 2,4 -> 4 - 2 = 2
And outputs:
1,2
Examples
-> 0,2 -> 0 1,2;0,1 -> 1 1,4;0,2;2,8 -> 2,4 1,4;0,2;2,-2 -> 2,-6
C++
Lectures C++
Practices
Assignment
Implement Conway's Game of Life using C++ and SFML.
See Practice 7 for an example of how to set up the project.
General info
The game consists of a grid of cells. Each cell is either alive or dead. Every cell interacts with its eight adjacent cells.
Before the start of the game, the user can select which cells are alive or dead.
The user can not change the states of the cells while the automation is running.
Once the game is started, the states of the cells in the next frame are calculated for all cells in the grid based on the following rules:
- If a live cell has fewer than two live neighbours, it dies,
- If a live cell has two or three live neighbours, it lives,
- If a live cell has more than five live neighbours, it dies,
- If a dead cell has three live neighbours, it becomes alive;
The game calculates and displays new frames until the automation is stopped.
Example usage
The user starts up the program and selects some cells to be alive.
The user presses a key to start the game.
The automation runs until the user presses a key to end the game or closes the program.
Exploits
Lectures
12. Week. Exception, Assembly language
https://echo360.e-ope.ee/ess/echo/presentation/c093e098-f789-4600-8b5c-f7995b3abfcb?ec=true
13. Week. Discussion about vulnerabilities and exploiting.
https://echo360.e-ope.ee/ess/echo/presentation/e479b230-24a1-48d4-bbb1-a4621e1231ac?ec=true
Practices
Practice 1
Assembly language
Setting up your environment:
- Create a new empty project,
- Add a main.cpp file,
- Under project "Properties -> C/C++ -> Code Generation" set Security Check to Disable Security Check;
To view disassembly:
- While debugging your program, open the disassembly window at Debug -> Windows -> Disassembly;
2. Create an integer variable i:
- Assign a value to i,
- Add 1 to i,
- Inspect the resulting assembly instructions;
3. Using what you have learned:
- Create an integer variable a,
- Using assembly, assign a value to a,
- Using assembly, add 1 to a,
- Verify, that the resulting value is correct;
4. Create a character array c to hold 3 characters:
- Fill in the array one by one,
- Inspect the resulting assembly instructions;
5. Using what you have learned:
- Create a integer array d to hold 2 ints,
- Using assembly, fill the array with data,
- Verify, that the array is filled with expected values;
6. Create a simple if:
- Subtract 1 from int i if i is equal to 2,
- Inspect the resulting assembly instructions;
7. Using what you have learned:
- Create an if conditional, that subtracts 1 from int a if int i and a are equal,
- Verify, that the conditional works as expected;
Practice 2
Assembly language continued, stack smashing
1. Create a function that takes no arguments
- Call the function
- Inspect the resulting assembly instructions;
2. Using what you have learned:
- Call the function using assembly,
- Verify that the function gets called;
3. Create an add function:
- The function should return the result of adding two integers passed in as parameters,
- Call the function and store the result in a variable,
- Inspect the resulting assembly instructions;
4. Using what you have learned:
- Create a subtract function,
- Use assembly to call the subtract function with different values and store the result in a variable,
- Verify, that the function call works as expected and that the value gets stored;
5. Create a function that internally holds an array of only one void* and takes a void* as a parameter:
- Write over the bounds of the array and observe what happens,
- Try passing different pointers to the function;
Assignment
Do some research on vulnerabilities related to the following topics:
- Buffer overflow (stack smashing, code injection, arc injection),
- Arithmetic over-/underflow,
- Memory leaks,
- Double free (and double delete),
- Formatted output,
- Concurrency,
- File I/O (symlinks, hardlinks);
Write an essay, that describes the causes, examples, and mitigation strategies for all of the topics.