The C++ Build Process - A Beginner's Guide to LLVM/Clang
Monday, April 5, 2021
Preamble If you’re a beginner and started to learn C++ very recently, read the chapters Preprocessor, Compiler, Linker, and Conclusions first. For the most part, all definitions and explanations in these chapters are compiler-independent. Come back to this document later after you moved on from writing terminal applications exclusively. The remaining chapters provide additional information by example of LLVM/Clang. See also the Appendix and Bibliography for further reading. Introduction For many developers who have primarily worked with interpreted languages such as Python or JavaScript in the past, learning C++ may seem like a daunting task at first: There’re many moving parts between your hello_world.…more
Debugging
Friday, April 24, 2020
Debugging Symbols When invoking a C++ compiler, several options/flags can be passed, including optimisation level, pre-processor defines, and whether to include debugging symbols in the binary. These symbols are effectively mappings between binary addresses and source code (file, line number, symbol name, etc), which a compatible debugger can use. Including debugging symbols dramatically increases the size of the resulting binary, often by orders of magnitude. A debugger (given an executable with debugging symbols) offers many useful features, such as stepping through code one line at a time, setting breakpoints, watching variables, changing values stored in memory, the entire call stack (when broken), etc.…more
Memory Management
Tuesday, February 25, 2020
C++ Memory Model Unlike higher level languages, the programmer is responsible for all memory management in C/C++. Automatic/stack-allocated memory is automatically cleaned up when it goes out of scope (hence automatic storage duration) and should always be preferred, even for managing heap memory (smart pointers). This is the basis of RAII. A definition like char x = 'a'; will result in the compiler storing enough space (1 byte) to hold one char.…more
Primitive Data Types
Monday, February 24, 2020
Primitive Data Types C/C++ being rather old languages, the width of types like int, long, etc have changed over time, from 16 bit CPUs to 32 bit and now 64 bit ones. This enables programs using such types to be generic and largely usable on all these target CPU architectures (potentially even ones in the future). The only generic type that has a guaranteed size is char (1 signed/unsigned byte on all modern platforms) - this is also a reason why files are read as arrays of chars (bytes).…more
February Challenges
Saturday, February 1, 2020
Number of words in a string Mini exercise: count the number of words in a string. bonus points: count the number of characters in each word Note: you can do both in one pass Short Exercise: Create a function to “bisect” a string given a delimiter: "id=fubar", '=' should return id and fubar. Prototype: std::pair<std::string, std::string> bisect(std::string_view str, char delim); pair can be found in <utility>. string_view instead of string const& allows for use of string literals / C strings without suffering a pointless string allocation.…more