C/C++
Table of Contents
- C++ is a superset of C with zero overhead abstraction, including OOP.
1. C Syntax
1.1. lvalue and rvalue
lvalue (locator value) has a fixed memory address,
and & operator can be used.
rvalue (read value) is temporary or literal that does not have persistent memory address.
1.2. Pointer
A variable stored in the memory that holds memory address.
int n = 5; int *p = &n # initalize ('*' = 'pointer to') an integer. e.g. p = 0x7999990D printf("%d", *p) # print the ('*' = 'dereference of') the pointer
The type of the pointer determines how to dereference the pointer.
Therefore, the pointer to unknown type, void *, must be typecasted before being used.
Pointer to a struct stores the first memory address
of the struct. The arrow operator p->m can be used
to access the member of the pointed struct.
It is equivalent to deferencing the struct and
useing the dot operator (*p).m
Pointer to an array of struct automatically increments by the multiple of the size of the struct, when incremented by 1.
1.3. Struct
Sequence of data bundled together.
1.4. Const
Define an unmodifiable variable
const int * pfixes the dereference*pint * const pfixes the pointerp
1.5. Macro
#define VAR VAL#define MACRO(<args>) EXPRESSION##can be used for literal concatenation
#include <...>searches the default locations.#include "..."searches current directory.
1.6. External and Internal Linkage
Function is always linked externally, the symbols are exposed and linked to the definition in the other file.
On the other hand variables are linked internally, that is, defined privately, by default.
Each file declares its own variable. extern T a keyword is used
in order to share the same variable.
2. C++ Syntax
C++ Standard is refered using the year it was published. e.g. C++23.
2.1. Reference
Pointer with compile-time reference counts.
- Reference to a reference is not allowed.
- References do not need to be dereferenced.
2.2. Range-For
Loop over the elements
std::vector<int> v; for (auto &x: v) { <body> }
2.3. Structured Binding
Unpacking the values into multiple variables
std::unordered_map<std::string, std::string> m; for (auto &[k, v]: m) { <body> }
Struct can be unpacked as well.
struct S { int a; std::string b; }; S s(); auto [x, y] = s();
2.4. Lambda Function
auto lambda = [](int x) { return x * x; };
Unnamed function object called functor is defined.
[]capture surrounding variables.[=]capture all by value,[x]capturexby value[&]capture all by reference,[&x]capturexby reference.
The return type is inferred, but it can also be specified explicitly:
auto double = [](int n) -> int { return 2*n; };
2.5. Class
struct is extended to have the exact same functionalities of class.
The difference is that the members of struct is public
and inherited publicly, by default.
2.5.1. Constructor
class Foo { Foo(<params>) : <member initialization> { <body> } #optional Foo(std::intializer_list<T>) { <body> } }
- member initalization happens during object construction
- It is a comma seperated list of constructor calls: e.g.
a(5), b{param1, param2}. - References and const variables are required to be initialized here.
- It is a comma seperated list of constructor calls: e.g.
Foo a{1, 2, 3};
Here, {} invokes the list initialization which is the second constructor. Narrowing (of the type) is not allowed when {} is used.
2.5.2. Destructor
Invoked automatically when the object gets out of the scope. Useful for memory management.
class Foo { ~Foo() { <body> } }
2.5.3. Inheritance
class Child : public Parent { ... }
publickeeps thepublicstate of the properties and methods.protectedchangepublicintoprotectedprivatechangepublicandprotectedintoprivate
2.6. C++ Header
- No
.hextension
Modern header files stored in
/usr/include/c++/<version>//usr/include/x86_64-linux-gnu/c++/<version>//usr/include/
std namespaces are used by the standard libraries.
2.7. Template
template <typename T> // function, class, using
Template is a blueprint that is instantiated on demand at compile-time.
2.7.1. SFINAE
- Substitution Failure Is Not An Error
When template instantiation fails, the compiler looks for another matching template instead of throwing an error. If every template fails, then error is thworn.
3. C Libraries
3.1. unistd.h
execfamilyexecl(const char *path, const char *arg, ..., (char *)NULL);variable-length argument listexecv(const char *path, char *const argv[]);array of stringsexec*p(const char *file, ...);PATH-searching variantexec*e(..., char *const envp[]);set environment variables
brk()set the program breaksbrk()increment the program break and return the previous address
4. C++ Libraries
4.0.1. iostream
std::coutthe output stream object<<operator outputs the right argument, and returnthis.
std::cin>>operator reads into the right argument, and returnthis.
4.0.2. string
std::stringwrapper object of a heap-allocated string
4.0.3. array
std::array<T, length>stack-allocated fixed-size array
4.0.4. vector
std::vector<T>heap-allocated dynamic array.push_back(int n)
4.0.5. utility
std::movethe ownership of the heap-allocated data is moved.- returning moved variable is unnecessary due to the Return Value Optimization (RVO) is already there.
4.0.6. memory
std::unique_ptr<T>wrapper of a pointer with automatically destructor according to the typeT.- The pointer returned by
mallocdoes not have destructor attached to it, and manualfreeis required.
- The pointer returned by
std::shared_ptr<T>wrapper of a pointer with reference count over multiple thread.
4.0.7. print
std::println
4.0.8. ranges
4.0.8.1. Range Adaptors
- C++20
std::ranges::views::orstd::views::
Create a view object out of iterable with a pipe (|) symbol.
e.g.
std::vector<int> numbers = {1, 2, 3, 4}; auto view = numbers | std::views::filter([](int n) {return n % 2 == 0; });
filter(<lambda>)transform(<lambda>)all(),counted()take(<n>),drop(<n>)join(),reverse()iota(<start>, <end>)generate sequencerepeat(<value>)(C++23)
5. Assembly
5.1. x8664
Register name with prefix
r: 64 bitse: 32 bits,
additionally
ax,bx,cx,dx: 16 bitsal,ah, …: 8 bits
Lables are defined with : at the end. A label can starts with ..
The label is equivalent to literal memory address.
5.1.1. GNU Syntax
- AT&T Syntax
Use prefixes for different kinds of symbols:
- Directives:
. - Register:
% - Immediate Value:
$,
Refer to memory like
disp(base,index,scale),
Binary operations act on the second argument.
Instruction with suffix
b(byte): 8 bitsw(word): 16 bitsl(long): 32 bitsq(quadword): 64 bits.
The data is read and wirrten as little endian, with the given address being the first address of the byte sequence.
5.1.2. Intel Syntax
- Directive
.intel_syntx
Do not have prefixes,
Refer to memory like
[expr]whereexprcan be arithmetic expression consists of register name and numbers,
Binary operations act on the first argument.
BYTE PTR, WORD PTR, DWORD PTR, QWORD PTR right after the instruction
indicates the width of the operation.
5.1.3. Instructions
| Instruction | Description |
|---|---|
mov A, B |
copy |
add A, B, sub A, B, mul A, B |
|
lea A, B |
load effective address: load the address of the memory reference |
call LABEL, ret |
subroutine |
5.2. syscall
It emits the instruction int 0x80 on Linux.
%raxsystem call number%rdi,%rsi,%rdx,%r10, … arguments for the call
%rax |
Name | Description |
|---|---|---|
| 0 | read | |
| 1 | write | |
| 2 | open | |
| 3 | close | |
| 60 | exit |
5.3. Assembler Directives
| Directive | Description |
|---|---|
.ascii "..." |
insert string data |
.asciz "..." |
insett string with zero attached |
.global SYMBOL |
expose SYMBOL to ld |
.text, .data, .bss |
set secion |
.equ VAR, VAL |
define a symbol |
.skip SIZE, FILL |
fill memory of SIZE with FILL |
5.4. Call Frame Information
- CFI
Keep track of the stack frames in the debugging section.
CFI is necessary when
- the compiler does optimization (
-fomit-frame-pointer) and not store former%rbp - exception handling
- need cross-platform consistency
5.5. Procedure Linkage Table
- PLT
The table contains the virtual addresses of the loaded shared objects, allowing different processes to use the shame physical memory for executables and shared objects.
For example, printf@plt initially points to the PLT setup code and
on the second time it is redirected to the loaded printf function directly.
6. Compilation
6.1. gcc
GNU Compiler Collection (formerly, GNU C Compiler)
- Takes care of all the preprocessing (
cpp), (proper) compilation (cc1), assembly (as), and linking (ld).
General Options
-oSet the name of the target, by default an executable-cStop before linking-SStop before assembly-Eonly perform preprocessing, and output to the standard out-gdebugging-ggdbadd debugging information forgdb
Linker Options
-l LIBRARYor-lLIBRARY-LDIRaddDIRto the list of library directory-sharedproduce a shared object
6.1.1. g++
gcc with the C++ library used by default when linking.
6.2. cpp
C Preprocessor
6.3. as
GNU Assembler
-ooutput file name-gdebug information
6.4. ld
GNU Linker (Loader)
-l<namespec>seach inlib<namespec>.aorlib<namespec>.so- It only searches for the symbols that the linker has seen so far. The library has to be specified after the source file.
- If
<namespec>looks like:filename,filenameis searched instead.
-( <filenames or -l options> -)search repeatedly until no undefined references left
6.5. clang
LLVM based C/C++ Compiler
7. make
- Parallel compilation tool
7.1. Syntax
7.1.1. Variable
Variables are simply defined
VAR = value
and can be accessed anywhere with $(VAR).
Variable expansion happens after the special character exchange.
7.1.2. Rule
A rule is defined as follows
target ...: prerequisite ...
recipe
...
Prerequisites are other targets, and recipes are any commands.
Recipes must starts with TAB, unless .RECIPEPREFIX is set otherwise.
The first target that does not begin with . is the default goal.
make checks if a given goal matches any of the targets, then executes the matched recipe.
7.1.2.1. Pattern Rule
Target can contain % that matches any nonempty string.
% can be reused in the prerequisites to refer back to the matched string.
7.1.2.2. Automatic Variables
$@the target name$<the first prerequisite$?space-separated list of newly modified prerequisites$^all prerequisites
7.1.3. Recipe
7.1.3.1. @
Stop the output of the command itself.
7.1.3.2. +
Force execution in the -n (no execution) mode.
7.2. Phony Targets
.PHONY: clean clean: rm *.o temp
Specify that the target file clean is not a file.
make does not check the existence of the phony target file.
8. cmake
8.1. CMakeLists.txt
project(<name> [LANGUAGES <lang>])define projectfind_package(<package name> [REQUIRED])looks for<package name>Config.cmakeand use the packagefind_library()add_library()add tartget library. e.g..o,.a,.soadd_executable()add target executabletraget_link_libraries()
8.2. Options
-S SRCDIR -B BUILDDIR -Dvar=valgenerate the build files--build BUILDDIR--install BUILDDIR--prefix CMAKE_INSTALL_PREFIXset the target directory for installation
9. Utilities
9.1. ldd
Print the shared objects required by program
9.2. strace
Trace system calls and signals
9.3. ltrace
Trace library calls
9.4. gdb
runframe,fsee the current stackinfo ...,i ...show informations[tep]execute one line of the source codec[ontinue]execute until a breakpointbr Nset a breakpoint
C-x 2 rotates through the TUI.