C/C++

Table of Contents

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 * p fixes the dereference *p
  • int * const p fixes the pointer p

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.1.1. rvalue Reference

  • T&&

It refers to rvalue, which enables the transfer of temporary objects.

The compiler has much more freedom about the exact machine code, and the copy of large object is often eliminated.

lvalue can be casted into rvalue by std::move in utility.

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. 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.4.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.
  • {} invokes the list initialization. Narrowing (of the type) is not allowed when {} is used.

2.4.2. Destructor

Invoked automatically when the object gets out of the scope. Useful for memory management.

class Foo {
  ~Foo() {
    <body>
  }
}

2.4.3. Inheritance

class Child : public Parent {
  ...
  }
  • public keeps the public state of the properties and methods.
  • protected change public into protected
  • private change public and protected into private

2.5. C++ Header

  • No .h extension

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.6. Template

template <typename T>
# function or class

Template is a blueprint that is instantiated on demand at compile-time.

2.6.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

  • exec family
    • execl(const char *path, const char *arg, ..., (char *)NULL); variable-length argument list
    • execv(const char *path, char *const argv[]); array of strings
    • exec*p(const char *file, ...); PATH-searching variant
    • exec*e(..., char *const envp[]); set environment variables
  • brk() set the program break
  • sbrk() increment the program break and return the previous address

4. C++ Libraries

4.0.1. iostream

  • std::cout the output stream object
    • << operator outputs the right argument, and return this.
  • std::cin
    • >> operator reads into the right argument, and return this.

4.0.2. string

  • std::string wrapper 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::move the 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 type T.
    • The pointer returned by malloc does not have destructor attached to it, and manual free is required.
  • std::shared_ptr<T> wrapper of a pointer with reference count over multiple thread.

4.0.7. print

  • std::println

5. Assembly

5.1. x8664

Register name with prefix

  • r: 64 bits
  • e: 32 bits,

additionally

  • ax, bx, cx, dx: 16 bits
  • al, 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 bits
  • w (word): 16 bits
  • l (long): 32 bits
  • q (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] where expr can 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.

  • %rax system 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

  • -o Set the name of the target, by default an executable
  • -c Stop before linking
  • -S Stop before assembly
  • -E only perform preprocessing, and output to the standard out
  • -g debugging
    • -ggdb add debugging information for gdb

Linker Options

  • -l LIBRARY or -lLIBRARY
  • -LDIR add DIR to the list of library directory
  • -shared produce 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

  • -o output file name
  • -g debug information

6.4. ld

GNU Linker (Loader)

  • -l<namespec> seach in lib<namespec>.a or lib<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, filename is 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:
    rrm *.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 project
  • find_package(<package name> [REQUIRED]) looks for <package name>Config.cmake and use the package
  • find_library()
  • add_library() add tartget library
  • add_executable() add target executable
  • traget_link_libraries()

8.2. Options

  • -S SRCDIR -B BUILDDIR -Dvar=val generate the build files
  • --build BUILDDIR
  • --install BUILDDIR
    • --prefix CMAKE_INSTALL_PREFIX set 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

  • run
  • frame, f see the current stack
  • info ..., i ... show information
  • s[tep] execute one line of the source code
  • c[ontinue] execute until a breakpoint
  • br N set a breakpoint

C-x 2 rotates through the TUI.

10. References

Created: 2025-09-14 Sun 20:12