diff --git a/slides/images/c-compilation.webp b/slides/images/c-compilation.webp new file mode 100644 index 0000000000000000000000000000000000000000..43ed296a89d2c4fc9a6321caec929a3962506445 Binary files /dev/null and b/slides/images/c-compilation.webp differ diff --git a/slides/images/k-and-r.jpg b/slides/images/k-and-r.jpg new file mode 100644 index 0000000000000000000000000000000000000000..7dc93e57c9995e17d9257f829997b280df14b784 Binary files /dev/null and b/slides/images/k-and-r.jpg differ diff --git a/slides/images/program-address-space.png b/slides/images/program-address-space.png new file mode 100644 index 0000000000000000000000000000000000000000..76cf8fcfc1800c5901824490441f72f1383b21da Binary files /dev/null and b/slides/images/program-address-space.png differ diff --git a/slides/pages/clang-1.md b/slides/pages/clang-1.md new file mode 100644 index 0000000000000000000000000000000000000000..e71bb111baa7d701497137b8f013470882f9ebfe --- /dev/null +++ b/slides/pages/clang-1.md @@ -0,0 +1,22 @@ +--- +title: C Programming Language review +layout: two-cols +--- + +# C Programming Language Review + +C is a **system programming language**, created for implementing the **Unix** operating system. + +## Spirit of C + +- Trust the programmer (run fast and crash fast) +- Keep the language small and simple +- Fast and portable (Scientific computation in python pretty fast? Well, check out the [implementation](https://github.com/python/cpython)) + +K&R is just about a **must-have** + +::right:: + +<div class="container flex justify-center"> + <img src="/images/k-and-r.jpg" class="block w-xs"/> +</div> diff --git a/slides/pages/clang-2.md b/slides/pages/clang-2.md new file mode 100644 index 0000000000000000000000000000000000000000..14d5fd99a41a2be36baa85d5304f89731b8a82ac --- /dev/null +++ b/slides/pages/clang-2.md @@ -0,0 +1,18 @@ +--- +title: C Key Concepts +--- + +# C Key Concepts + +- Pointers +- Arrays +- Implications for Memory Management + +All of the above are **unsafe**. You can shoot yourself in the foot easily. Full of **undefined behaviours** -- source of the most errors. + +#### Understanding The Compilation Process + +<div class="container flex justify-center mt-5"> + <img src="/images/c-compilation.webp" class="block w-1/2"/> +</div> + diff --git a/slides/pages/clang-3.md b/slides/pages/clang-3.md new file mode 100644 index 0000000000000000000000000000000000000000..48f12a90afd27a37de3e52b06aa81eaf31ef425d --- /dev/null +++ b/slides/pages/clang-3.md @@ -0,0 +1,48 @@ +--- +title: Pointers in C +layout: two-cols +--- + +# Pointers in C + +Consider memory to be a huge single array. + +- Each cell has an address associated with it. +- Each cell can store some value. +- **Pointers** are cells that contains the address of a cell. +- Pointers have **type** (they point to this type of data). +- Pointers can point to almost anything: variables, pointers, even functions. +- **Null Pointer** is special, don't read or write it. (Undefined behaviour!) +- **Array variables are almost identical to pointers**. +- Array in function arguments is just a pointer without **size** +- String in C is just an array of `char`. + +::right:: + +```c +/* Basic Syntax */ +int *ptr; int a = 10; +ptr = &a; *ptr = 20; + +/* Pointer to a Pointer */ +int **pptr = &ptr; +**pptr = 30; + +/* Function Pointer */ +int a = 10; int b = 20; +int fn(int *a, int *b) { + int temp = *a; + *a = *b; + *b = temp; +} +int (*swap) (int *, int *) = &fn; +(*swap)(&a, &b); + +/* Array and Pointer */ +int arr[3] = {1, 2, 3}; +// arr[1] == *(a + 1) + +/* String */ +char str1[] = "I am a character array"; +char *str2 = "I am a string literal"; +``` diff --git a/slides/pages/clang-4.md b/slides/pages/clang-4.md new file mode 100644 index 0000000000000000000000000000000000000000..e5aa37ffcd87564dee782d53d9acae7db18c07fb --- /dev/null +++ b/slides/pages/clang-4.md @@ -0,0 +1,26 @@ +--- +title: C Memory Management +layout: two-cols +--- + +# C Memory Management + +Program's address space contains **stack**, **heap**, **static data**, **code**. Dynamic memory management refers to heap memory management. + +### Support Functions in `<stdlib.h>` + +- `malloc()`: allocate a block of **uninitialized** memory +- `calloc()`: allocate a block of **zeroed** memory +- `free()`: free previously allocated memory +- `realloc()`: expand or shrink the previously allocated memory + +### Tools + +Use [valgrind](https://valgrind.org/) to find out memory related bugs. + +::right:: + +<div class="container flex justify-center"> + <img src="/images/program-address-space.png" class="block w-xs"/> +</div> + diff --git a/slides/pages/clang.md b/slides/pages/clang.md deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/slides/slides.md b/slides/slides.md index 7a946de9326f64945101d785c16199e212016cc0..f29ad940db476410f8674cba666d835c7b0e7870 100644 --- a/slides/slides.md +++ b/slides/slides.md @@ -51,5 +51,20 @@ src: ./pages/git.md --- transition: slide-left -src: ./pages/clang.md +src: ./pages/clang-1.md +--- + +--- +transition: slide-left +src: ./pages/clang-2.md +--- + +--- +transition: slide-left +src: ./pages/clang-3.md +--- + +--- +transition: slide-left +src: ./pages/clang-4.md ---