Skip to content
Snippets Groups Projects
Commit 484ed08c authored by Mactavish's avatar Mactavish
Browse files

update slides

parent e26e69ef
No related branches found
No related tags found
No related merge requests found
slides/images/c-compilation.webp

8.12 KiB

slides/images/k-and-r.jpg

50.3 KiB

slides/images/program-address-space.png

22 KiB

---
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>
---
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>
---
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";
```
---
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>
......@@ -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
---
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment