🏠

C Basics

https://pambrose.github.io/myslides/c.html

🕯 Overview of Slides

  • What is C?
  • Syntax
  • Type System
  • Number Systems Review
  • Variables in Memory
  • Arrays and Pointers
  • Strings
  • Stack and Heap
  • Avoiding Trouble with Memory
  • Structs
  • Function Pointers and Object-Oriented C
  • Code Sources

🔦 What is C?

  • Dennis Ritchie at AT&T Bell Labs in 1972
  • Speedcode -> FORTRAN -> ALGOL 58 -> ALGOL 60 -> CPL -> BCPL -> B -> C
  • Influenced countless languages, including: C++, C#, Java, Python, Go, and Obj-C
  • Low-level systems language
  • Static type system with weak enforcement
  • Architecture-specific language -- not WORA
  • C source files are compiled to object files
  • Programs and libraries

🌅 Hello World

  • #include preprocessing directives
  • .c and .h files
  • for, if, {}, quotes, and ;
  • <stdio.h>
  • Return success value

🚌 Basic Types

  • char, int, float and double
  • Modifiers: signed, unsigned, short, and long
  • Summary of types

🧮 Decimal, Octal, Hex, and Binary

📬 Hex Addresses

🔬 Variables in Memory

🚌 Arrays

🚌 Arrays (1)

🔬 Arrays in Memory

🚌 Arrays (2)

🔬 Arrays in Memory

☝ Pointers

☝ Pointers (1)

🔬 Pointers in Memory

☝ Pointers (2)

🔬 Pointers in Memory

📱 Call by Value and Reference

☝ Pointers to Arrays

🔬 Pointers to Arrays in Memory

☝ Pointers and sizeof()

🧶 Strings

🧶 Strings (1)

🔬 Strings in Memory

🧶 Strings (2)

🔬 Strings in Memory

🧶 Strings (3)

🧶 Strings (4)

🔬 Strings in Memory

🧶 Strings (5)

The Stack and Heap

More info on Stack and Heap Memory

📚 Call Stacks (1)

📚 Call Stacks (2)

📚 Call Stacks (3)

📚 The Stack

Allocate variable a for main
Allocate b for main and store -3
Allocate c for main and store 12345
Allocate p for main and store address of b
Allocate variable a for hello and store 100
Deallocate the stack memory of hello and return 100 to main
Allocate d for main and store 100
Deallocate the stack memory of main and return 0

⛰ The Heap

Allocate an integer with default value 0 on the heap, allocate p on main's stack to store the address of the integer
Allocate a Cube with default width 20 on the heap, allocate c1 on main's stack to store the address of the Cube
Allocate c2 on main's stack and store a copy of c1
Call method setLength on c2, changes the width of the Cube pointed by both c1 and c2
Deallocate stack memory of main and return 0

⛰ Heap Calls

⛰ Heap Calls (1)

🔬 malloc() Memory

⛰ Heap Calls (2)

⛰ Initialized Heap Memory

🔁 Reallocating Heap Memory

⚠ Avoiding Trouble with Memory

⚠ Calls to free() (1)

⚠ Calls to free() (2)

⚠ Return an Array

🦺 Return an Array

⚠ Memory Leak

🧶 String Functions

🧶 Arrays of Strings

🧶 Arrays of Strings (1)

🔬 String Array in Memory

🧶 Arrays of Strings (2)

🎒 C structs

🎒 C structs

🎒 C struct Arrays

🎒 C struct Parameters

👉 Function Pointers

👉 Function Pointers

👉 Higher Order Functions (1)

👉 Higher Order Functions (2)

🌵Object-Oriented C

🏦 Understanding Exploits