finished formatting md file
This commit is contained in:
parent
f8b6995add
commit
f20b2d9e22
1 changed files with 33 additions and 33 deletions
|
|
@ -21,8 +21,8 @@ C standard libraries hide some functions unless you *opt in*.
|
||||||
|
|
||||||
Without it:
|
Without it:
|
||||||
|
|
||||||
* Some systems won’t expose those functions
|
- Some systems won’t expose those functions
|
||||||
* You’ll get mysterious warnings or missing symbols
|
- You’ll get mysterious warnings or missing symbols
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -37,8 +37,8 @@ Without it:
|
||||||
|
|
||||||
In C:
|
In C:
|
||||||
|
|
||||||
* Headers are **contracts**
|
- Headers are **contracts**
|
||||||
* If it’s not included, the compiler assumes *nothing*
|
- If it’s not included, the compiler assumes *nothing*
|
||||||
|
|
||||||
**Rule of thumb:**
|
**Rule of thumb:**
|
||||||
|
|
||||||
|
|
@ -58,9 +58,9 @@ Example:
|
||||||
|
|
||||||
### What this teaches you
|
### What this teaches you
|
||||||
|
|
||||||
* C has **no runtime reflection**
|
- C has **no runtime reflection**
|
||||||
* Portability happens at **compile time**
|
- Portability happens at **compile time**
|
||||||
* The preprocessor literally removes code before compilation
|
- The preprocessor literally removes code before compilation
|
||||||
|
|
||||||
Think of it as:
|
Think of it as:
|
||||||
|
|
||||||
|
|
@ -82,11 +82,11 @@ static char *dup_or_unknown(const char *s)
|
||||||
|
|
||||||
This function enforces a **contract**:
|
This function enforces a **contract**:
|
||||||
|
|
||||||
* Every getter:
|
- Every getter:
|
||||||
|
|
||||||
* returns a valid `char *`
|
- returns a valid `char *`
|
||||||
* never returns `NULL`
|
- never returns `NULL`
|
||||||
* always returns heap memory
|
- always returns heap memory
|
||||||
|
|
||||||
### Why that matters
|
### Why that matters
|
||||||
|
|
||||||
|
|
@ -124,9 +124,9 @@ Rules being followed:
|
||||||
|
|
||||||
Contrast this with C++:
|
Contrast this with C++:
|
||||||
|
|
||||||
* No destructors
|
- No destructors
|
||||||
* No smart pointers
|
- No smart pointers
|
||||||
* No safety net
|
- No safety net
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -140,15 +140,15 @@ static char buf[256];
|
||||||
|
|
||||||
But that would:
|
But that would:
|
||||||
|
|
||||||
* Break thread safety
|
- Break thread safety
|
||||||
* Break reentrancy
|
- Break reentrancy
|
||||||
* Make functions non-composable
|
- Make functions non-composable
|
||||||
|
|
||||||
Dynamic allocation makes your functions:
|
Dynamic allocation makes your functions:
|
||||||
|
|
||||||
* Reusable
|
- Reusable
|
||||||
* Testable
|
- Testable
|
||||||
* Library-quality
|
- Library-quality
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -163,9 +163,9 @@ Each platform teaches a lesson:
|
||||||
/proc/uptime
|
/proc/uptime
|
||||||
```
|
```
|
||||||
|
|
||||||
* Text parsing
|
- Text parsing
|
||||||
* Line-by-line scanning
|
- Line-by-line scanning
|
||||||
* Defensive string handling
|
- Defensive string handling
|
||||||
|
|
||||||
### macOS / BSD
|
### macOS / BSD
|
||||||
|
|
||||||
|
|
@ -173,9 +173,9 @@ Each platform teaches a lesson:
|
||||||
sysctl()
|
sysctl()
|
||||||
```
|
```
|
||||||
|
|
||||||
* Structured kernel APIs
|
- Structured kernel APIs
|
||||||
* Buffer-size negotiation
|
- Buffer-size negotiation
|
||||||
* Integer & struct-based data
|
- Integer & struct-based data
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -188,9 +188,9 @@ difftime(now, boottime.tv_sec);
|
||||||
|
|
||||||
Why this matters:
|
Why this matters:
|
||||||
|
|
||||||
* `time_t` may not be an integer
|
- `time_t` may not be an integer
|
||||||
* You *never* subtract times directly
|
- You *never* subtract times directly
|
||||||
* `difftime()` handles portability
|
- `difftime()` handles portability
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -204,10 +204,10 @@ free(user);
|
||||||
|
|
||||||
Notice:
|
Notice:
|
||||||
|
|
||||||
* No logic
|
- No logic
|
||||||
* No parsing
|
- No parsing
|
||||||
* No platform checks
|
- No platform checks
|
||||||
* No error handling clutter
|
- No error handling clutter
|
||||||
|
|
||||||
All complexity lives *outside* `main()`.
|
All complexity lives *outside* `main()`.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue