2019-07-02 14:35:02 +00:00
## Purpose
The purpose of this repository is to provide code examples that can be compiled and run for people reading my PhD thesis.
These examples codes are **not** for production and often are simplified with regard of technical specificities (for example, but not limited to, perfect forwarding).
## Usage
```bash
mkdir build
cd build
cmake ..
make
```
then run any example binary, located inside the `build/examples/` directory.
2019-07-02 15:21:34 +00:00
## Remarks
### Assembler code
Some examples are meant to show the assembler code they generate.
To see that, one can use for example https://godbolt.org/ or GDB.
With GDB:
```bash
gdb -q -ex 'disassemble main' -ex q "${program_to_execute}"
```
Important note:
if using `cmake` to produce the Makefile, you must call it with at least:
```bash
cmake -DCMAKE_BUILD_TYPE=Release ..
```
or the examples will be compiled without the optimization level 2.
### use of `volatile`
When a value is required to show something, it is necessary to prevent the compiler to
optimize out this value. To achieve this, one can read the value from a file, for example,
but it will generate more assembler code.
Another solution is the `volatile` type modifier that forbids optimization like caching or
other optimizations like:
```cpp
int main() {
int volatile v0 = 5, v1 = 7;
return v0 + v1;
}
```
Without `volatile` , the addition is done by the compiler and `main` returns directly `12` .
With `volatile` , the generated assembler code will contain an `add` instruction, even with `-O2` .
### `std::printf` instead of `std::cout`
C++ iostreams cause lots of assembler code, for that reason, when an example is expected to be
possibly used by looking at its generated assembler code, iostreams are avoided and replaced by
`printf` .