Latest / CyberSecurity Summary / C: From Theory to Practice
Transcript
- 0:00Speaker 1: Imagine you were handed the keys to a vehicle, and
- 0:05Speaker 1: not just any vehicle.
- 0:06Speaker 2: Either, right, A serious machine.
- 0:07Speaker 1: Yeah, exactly. We're talking about an absolute masterpiece of engineering,
- 0:12Speaker 1: like a high performance Formula one car. It's capable of
- 0:15Speaker 1: just mind bending speed, unparalleled agility, total responsiveness, total responsiveness. Yeah.
- 0:22Speaker 1: But as you strap into the driver's seat, you notice
- 0:25Speaker 1: something is missing. There are no seat belts, none at all,
- 0:29Speaker 1: no airbags. The traction control has been completely ripped out,
- 0:33Speaker 1: and the steering wheels wired directly to the front axle
- 0:36Speaker 1: without a single ounce of power assist.
- 0:38Speaker 2: It's well, it's a machine built purely for raw performance.
- 0:41Speaker 2: But that means it demands absolute perfection from you, the driver. Right,
- 0:45Speaker 2: like one twitch of your wrist, you know, one millimeter
- 0:48Speaker 2: of overcorrection and you are going straight into the wall
- 0:51Speaker 2: at two hundred.
- 0:51Speaker 1: Miles an hour and the car won't stop you.
- 0:53Speaker 2: No, it assumes you meant to turn into that wall.
- 0:56Speaker 1: That terrifying, exhilarating race car is exactly what we are
- 1:00Speaker 1: exploring today. Welcome to our deep.
- 1:02Speaker 2: Dive, Thanks for having me.
- 1:03Speaker 1: Today. We are looking at the C programming language, utilizing
- 1:07Speaker 1: the comprehensive breakdown found in the second edition of the
- 1:11Speaker 1: textbook C From Theory to Practice by George Stalikas and
- 1:16Speaker 1: Nicolaus Detzelukas.
- 1:17Speaker 2: It's a fantastic resource, it really is.
- 1:19Speaker 1: And our mission for you today is not to just
- 1:22Speaker 1: recite a software manual or like quiz you on semi
- 1:25Speaker 1: Coolin's ut. Whether you are cramming for a computer science
- 1:28Speaker 1: exam or you're just insanely curious about the invisible architecture
- 1:32Speaker 1: of your digital world, we want to extract the hidden gotchaes.
- 1:37Speaker 2: The invisible traps exactly.
- 1:38Speaker 1: We are hunting for those surprising logic traps and foundational
- 1:42Speaker 1: aha moments of C that will fundamentally make you a
- 1:44Speaker 1: sharper thinker.
- 1:44Speaker 2: Because to understand C is to understand the absolute bedrock
- 1:48Speaker 2: of modern computing. I mean, it is a language of incredible,
- 1:51Speaker 2: unmatched power. But as that race car visual suggests, it's
- 1:55Speaker 2: a landscape entirely devoid of safety rails.
- 1:58Speaker 1: So before we start looking under the hood that the
- 2:00Speaker 1: actual code, we should probably figure out why anyone would
- 2:04Speaker 1: build a vehicle with no brakes, Like, why was this
- 2:07Speaker 1: language designed this way? And why decades later is it
- 2:11Speaker 1: still running the world.
- 2:12Speaker 2: Well, the origin story takes us back to the early
- 2:14Speaker 2: nineteen seventies at Bell Labs Okay, Dennis Ritchie alongside a
- 2:18Speaker 2: team of just brilliant engineers. They needed a tool for
- 2:22Speaker 2: a very specific, monumental task. They were rewriting the Unix
- 2:27Speaker 2: operating system.
- 2:28Speaker 1: Wow okay, and at the time, Unix was primarily written
- 2:31Speaker 1: in assembly language.
- 2:32Speaker 2: Exactly.
- 2:33Speaker 1: I've always pictured assembly language as essentially, you know, whispering
- 2:38Speaker 1: raw ones and zeros directly to the computer's processor.
- 2:41Speaker 2: That's practically what it is.
- 2:42Speaker 1: It's incredibly low level, raw machine instructions. That sounds like
- 2:45Speaker 1: a torturous way to write a massive operating system.
- 2:48Speaker 2: Oh, torture is putting it mildly. Programs written in assembly
- 2:51Speaker 2: are notoriously difficult to debug, almost impossible to maintain, and
- 2:55Speaker 2: trying to port them to a different type.
- 2:57Speaker 1: Of computer you'd have to start over.
- 2:58Speaker 2: Yeah, it meant rewriting the whole thing from scratch. So
- 3:02Speaker 2: Richie needed something that would make these tasks manageable, a
- 3:05Speaker 2: language that provided human readable structure but still retained that intimate,
- 3:10Speaker 2: low level control over the computer's physical hardware. He evolved
- 3:14Speaker 2: an earlier language called B and logically enough named the successor.
- 3:18Speaker 1: C pragmatic naming. I love it. But a language created
- 3:23Speaker 1: in a lab doesn't just instantly become the global standard, right.
- 3:26Speaker 2: Oh, not at all.
- 3:27Speaker 1: The text mentions that as C exploded in popularity, different
- 3:31Speaker 1: companies started making their own C compilers to read the code,
- 3:35Speaker 1: and things got pretty chaotic.
- 3:37Speaker 2: Chaos is definitely the right word. In those early years,
- 3:40Speaker 2: programmers relied on a famous book by Brian Kernian and
- 3:42Speaker 2: Dennis Ritchie, the K and R Book, right yep, often
- 3:45Speaker 2: just called the K and R Book. They used it
- 3:47Speaker 2: as their definitive guide. But a textbook isn't a strict
- 3:50Speaker 2: technical standard. It contained ambiguities.
- 3:53Speaker 1: Oh, I see.
- 3:54Speaker 2: You could write a piece of code, feed it into
- 3:56Speaker 2: one company's compiler and get a working program. Feed that
- 4:01Speaker 2: exact same text into a competitor's compiler, and the resulting
- 4:04Speaker 2: program might behave entirely differently.
- 4:07Speaker 1: Wait, so, if this ambiguity was causing so much chaos
- 4:10Speaker 1: when the American National Standards Institute ANSI finally stepped in
- 4:14Speaker 1: to formally standardize the language in nineteen eighty nine, why
- 4:18Speaker 1: didn't they build safety rails into it? That's a great question, Like,
- 4:22Speaker 1: if they are writing the universal rule book, why preserve
- 4:24Speaker 1: the danger?
- 4:25Speaker 2: Well, if we connect this to the bigger picture. Restricting
- 4:28Speaker 2: the language would defeat its entire purpose. The philosophy of
- 4:31Speaker 2: ANSIIC and C in general is built on one core tenant,
- 4:36Speaker 2: which is to trust the programmer implicitly.
- 4:38Speaker 1: Okay, implicitly right.
- 4:40Speaker 2: The language assumes that you, the human typing on the keyboard,
- 4:43Speaker 2: know exactly what you were doing. If you tell the
- 4:45Speaker 2: system to overwrite its own memory, it assumes you have
- 4:48Speaker 2: a brilliant architectural reason for doing so. It won't stop you,
- 4:52Speaker 2: it won't second guess you at all.
- 4:53Speaker 1: We aren't giving you safety rails because you might actually
- 4:55Speaker 1: need to drive on the wall. Okay, I get it.
- 4:58Speaker 1: But if C trusts us implicitly, how do we get
- 5:02Speaker 1: from a text file full of our potentially flawed ideas
- 5:07Speaker 1: to a functioning piece of software.
- 5:09Speaker 2: This requires looking at the C program life cycle, which
- 5:12Speaker 2: is a surprisingly involved four step journey.
- 5:14Speaker 1: Okay, what me through it.
- 5:15Speaker 2: First, you use a text editor to write your source code.
- 5:18Speaker 2: Then a program called the compiler translates that human readable
- 5:22Speaker 2: text into an object file, essentially translating your English like
- 5:26Speaker 2: commands into raw machine language. Next, a critical piece called
- 5:31Speaker 2: the linker takes that object file and connects it with
- 5:34Speaker 2: any standard library functions you use, and finally that produces
- 5:38Speaker 2: the executable file you actually run.
- 5:40Speaker 1: I want to pause on that third step, the linker,
- 5:43Speaker 1: because I think people here compile all the time, but
- 5:45Speaker 1: link gets completely glossed over.
- 5:46Speaker 2: It really does.
- 5:47Speaker 1: What is the linker actually doing In a practical sense.
- 5:50Speaker 2: Think of building a house. Your compiler is the team
- 5:53Speaker 2: that builds the physical structure, the walls, the roof, the pipes,
- 5:58Speaker 2: but those pipes don't have any water in them yet.
- 6:00Speaker 2: The linker is the city worker who comes in and
- 6:03Speaker 2: connects your house's plumbing to the municipal water grid.
- 6:06Speaker 1: Oh that's a great analogy.
- 6:07Speaker 2: Because in C you rarely write every single function from scratch.
- 6:12Speaker 2: You rely on standard libraries for things like printing to
- 6:15Speaker 2: a screen or doing math. The linker stitches your custom
- 6:19Speaker 2: code to those massive pre existing libraries so your program
- 6:23Speaker 2: can actually interact with the computer.
- 6:25Speaker 1: Okay, let's unpack this. That makes perfect sense. But let's
- 6:28Speaker 1: rewind to the compiler, because that's where I think a
- 6:30Speaker 1: dangerous illusion lives.
- 6:31Speaker 2: Oh.
- 6:31Speaker 1: Absolutely, it feels like the compiler is this smart, benevolent
- 6:36Speaker 1: guardian checking your work. Like if the compiler successfully builds
- 6:40Speaker 1: your object file, without throwing any error messages, your program
- 6:44Speaker 1: is good to go. Yeah.
- 6:45Speaker 2: That is the very first grand illusion of C. A
- 6:48Speaker 2: successful compilation simply means your syntax is valid.
- 6:51Speaker 1: Just the syntax, right.
- 6:52Speaker 2: It only checks the grammar of the code. It does
- 6:54Speaker 2: absolutely nothing to verify your logic.
- 6:57Speaker 1: I always compare the compiler to a A six spellchecker
- 7:01Speaker 1: in a word processor. Like the spellchecker confirms you spell
- 7:04Speaker 1: the word UK correctly. The grammar is valid. Yeah, but
- 7:08Speaker 1: it has absolutely no idea. If your sentence meant duck
- 7:12Speaker 1: as in the aquatic bird, or duck as in quickly
- 7:15Speaker 1: lower your head to avoid a flying object.
- 7:18Speaker 2: You're close. But the reality of C is actually a
- 7:21Speaker 2: bit more literal than a spellchecker Husso a modern spellchecker
- 7:25Speaker 2: might look at the context and suggest to fix. The
- 7:27Speaker 2: C compiler doesn't care enough to suggest anything.
- 7:30Speaker 1: Wow, okay.
- 7:30Speaker 2: If you write a program that says print one if
- 7:33Speaker 2: the number is less than five, but you accidentally typed
- 7:36Speaker 2: the greater than symbol, the compiler happily builds it.
- 7:39Speaker 1: It just lets it through.
- 7:40Speaker 2: The syntax is flawless, but the logic is entirely.
- 7:43Speaker 1: Inverted, and that inverted logic is what we call a
- 7:45Speaker 1: bug which has a fantastic literal origin story involving Grace Hopper.
- 7:50Speaker 2: Right, Yes, the pioneering computer scientist. She and her team
- 7:53Speaker 2: were investigating a hardware malfunction in an early electro mechanical
- 7:56Speaker 2: computer and found a literal physical moth trapped inside.
- 8:00Speaker 1: A relay, a real bug.
- 8:01Speaker 2: Yeah. They taped the moth into the log book, noting
- 8:04Speaker 2: they had literally debugged the machine.
- 8:06Speaker 1: That's amazing. Yeah, But in c our bugs are invisible.
- 8:11Speaker 1: They're entirely conceptual. Sometimes the language's own syntax rules create
- 8:16Speaker 1: traps that hide these bugs from the compiler. Yes, let's
- 8:20Speaker 1: look at the nest to comment trap.
- 8:21Speaker 2: Oh, this is a classic. So comments are notes you
- 8:24Speaker 2: leave for yourself in the code. You're telling the compiler
- 8:27Speaker 2: ignore everything here.
- 8:28Speaker 1: Right.
- 8:29Speaker 2: In C, you start a comment block with a forward
- 8:31Speaker 2: slash and an asterisk, and you end it with an
- 8:33Speaker 2: asterisk and a forward slash.
- 8:35Speaker 1: See's fool proof. I tell it to stop reading, and
- 8:37Speaker 1: then I tell it to start reading again.
- 8:38Speaker 2: The trap springs when you try to put a comment
- 8:40Speaker 2: inside another comment, nesting them. The strict rule of the
- 8:44Speaker 2: C compiler is that a comment ends the absolute moment
- 8:47Speaker 2: it sees the very first closing asterisk slash.
- 8:50Speaker 1: Wait.
- 8:50Speaker 2: Really, yes, it doesn't keep count of how many times
- 8:53Speaker 2: you open a comment block.
- 8:55Speaker 1: Uh okay. So if I have fifty lines of code
- 8:58Speaker 1: that I want to temporarily disable, so I wrapped the
- 9:01Speaker 1: whole giant block in comment symbols, but deep inside that
- 9:03Speaker 1: block online twenty, I had previously written a tiny little
- 9:07Speaker 1: note to myself with its own comment symbols.
- 9:10Speaker 2: The compiler hits that tiny note's closing symbol online twenty,
- 9:14Speaker 2: declares the massive comment block finished, and immediately begins executing
- 9:18Speaker 2: lines twenty one through fifty as live, active code.
- 9:22Speaker 1: Oh wow, And if lines twenty one through fifty happen
- 9:25Speaker 1: to be grammatically valid syntax, the compiler won't complain at all,
- 9:29Speaker 1: not a peep. It will just build a completely different,
- 9:31Speaker 1: deeply flawed program exactly.
- 9:33Speaker 2: It assumes you wanted those lines to be active.
- 9:36Speaker 1: Terrifying. So if syntax itself is full of invisible logic traps,
- 9:40Speaker 1: what happens when we successfully compile the code and start
- 9:43Speaker 1: dealing with the computer's memory.
- 9:45Speaker 2: Well, this is where human intuition violently collides with machine reality.
- 9:49Speaker 2: Variables in C are at their core just names for
- 9:52Speaker 2: specific one byte memory cells. In your computer's ramky, you
- 9:55Speaker 2: are just assigning a human readable label to a physical
- 9:58Speaker 2: address in the hardware, and the type.
- 10:00Speaker 1: Of variable you declare, like an integer or a decimal,
- 10:03Speaker 1: tells a computer how much physical memory to grab and
- 10:05Speaker 1: how to interpret the ones and zeros inside it.
- 10:07Speaker 2: Right, Let's test your intuition on how that memory is interpreted.
- 10:11Speaker 2: Suppose you declare a floating point.
- 10:12Speaker 1: Variable a number with a decimal.
- 10:14Speaker 2: Yeah, exactly, You write float a equals three point one.
- 10:18Speaker 1: Done. I have a box labeled A, and I've put
- 10:20Speaker 1: three point one inside it.
- 10:21Speaker 2: Okay. Later in your code, you write an if statement.
- 10:24Speaker 2: You ask the program if A equals three point one?
- 10:27Speaker 2: Does it equal three point one?
- 10:28Speaker 1: Well, I just put three point one in the box,
- 10:30Speaker 1: so obviously yes it's true, and c.
- 10:33Speaker 2: That statement will shockingly return false.
- 10:35Speaker 1: Wait, what if I definitively set it variable to three
- 10:38Speaker 1: point one? How can the computer possibly look at it
- 10:41Speaker 1: a microsecond later and tell me it's not three point.
- 10:43Speaker 2: One because of the translation between our base ten decimal
- 10:46Speaker 2: system and the computer's base two binary system. Think about
- 10:50Speaker 2: how we represent the fraction one third in decimals.
- 10:52Speaker 1: It's an endless point three to three three.
- 10:54Speaker 2: Three, right, you can never write it perfectly. The exact
- 10:56Speaker 2: same thing happens to computers with certain decimals like three
- 10:59Speaker 2: point one. Binary cannot represent three point one perfectly, so
- 11:02Speaker 2: the machine stores a value that is incredibly microscopically close
- 11:05Speaker 2: to three point one.
- 11:06Speaker 1: Like three point one zero zero zero zero zero zero
- 11:09Speaker 1: zero zero zero zero zero one.
- 11:11Speaker 2: Yes, So when you ask the compiler if aqua equals
- 11:14Speaker 2: three point one, the machine compares the literal perfect three
- 11:17Speaker 2: point one. You just type against the slightly imprecise representations stored.
- 11:22Speaker 1: In memory, and they don't match.
- 11:23Speaker 2: They don't perfectly match, So if strict equality fails, you
- 11:26Speaker 2: should almost never use the double equal sign to compare
- 11:29Speaker 2: floating point numbers. You always have to check if the
- 11:31Speaker 2: difference between them is smaller than a tiny acceptable margin.
- 11:35Speaker 1: Here's where it gets really interesting, because it's completely rational
- 11:39Speaker 1: from the machine's perspective, but deeply unintuitive for a human.
- 11:43Speaker 1: But I know it gets stranger. Hit me with the
- 11:45Speaker 1: sign versus unsigned trap.
- 11:47Speaker 2: Let's test your intuition again. We declare a sign into JERM,
- 11:50Speaker 2: meaning it can be negative or positive. We set it
- 11:53Speaker 2: to negative twenty. Then we declare an unsigned into JERM,
- 11:56Speaker 2: meaning it can only ever be positive. We set it
- 11:58Speaker 2: to two hundred.
- 12:00Speaker 1: Negative twenty positive two hundred. Got it.
- 12:02Speaker 2: Now we write a simple condition is negative twenty less
- 12:04Speaker 2: than two hundred, which one is.
- 12:06Speaker 1: Smaller in any universe. Negative twenty is a vastly smaller
- 12:09Speaker 1: number than positive two hundred.
- 12:11Speaker 2: In C. If you compare those two specific variables, the
- 12:14Speaker 2: compiler evaluates that statement is false. Negative twenty is not
- 12:18Speaker 2: less than two hundred.
- 12:20Speaker 1: That feels broken. What on earth is the machine doing
- 12:23Speaker 1: behind the scenes to reach that conclusion.
- 12:25Speaker 2: What's fascinating here is that it's applying implicit arithmetic conversions.
- 12:29Speaker 2: C has very strict rules for mixing different data types
- 12:32Speaker 2: in an equation. When you force a comparison between a
- 12:35Speaker 2: signed integer and an unsigned integer, the compiler silently promotes
- 12:39Speaker 2: the sign integer to match the unsigned one.
- 12:42Speaker 1: So it takes my negative twenty and just strips the
- 12:44Speaker 1: minus sign off. It becomes a positive twenty.
- 12:46Speaker 2: It's much weirder than that. It doesn't strip the sign
- 12:48Speaker 2: it changes how it reads the underlying binary code. Computers
- 12:52Speaker 2: store negative numbers using a system called two's compliment.
- 12:55Speaker 1: Two's compliment, okay, think.
- 12:56Speaker 2: Of the leftmost bit in the binary sequence like a
- 12:59Speaker 2: toggle switch in a sign number. If that leftmost switch
- 13:02Speaker 2: is flipped on, the computer reads that as the rule
- 13:04Speaker 2: this entire number is negative.
- 13:06Speaker 1: Okay, the switch tells the system how to behave.
- 13:09Speaker 2: But when the compiler promotes that number to unsigned, it
- 13:12Speaker 2: suddenly forgets the negative rule. It looks at that exact
- 13:15Speaker 2: same leftmost switch being flipped on, but because unsigned numbers
- 13:19Speaker 2: can't be negative, it interprets that switch simply as a
- 13:23Speaker 2: massive numeric value.
- 13:24Speaker 1: Wait, so what happens to my negative twenty when that
- 13:27Speaker 1: leftmost bit gets misread as a giant positive value.
- 13:30Speaker 2: In a typical thirty two bit system, that negative twenty
- 13:33Speaker 2: suddenly transforms into a positive number right around four point
- 13:36Speaker 2: two billion.
- 13:37Speaker 1: Four point two billion.
- 13:38Speaker 2: Yes, so the machine is actually evaluating the statement is
- 13:41Speaker 2: four point two billion less than two hundred and naturally
- 13:45Speaker 2: mathematically it answers false.
- 13:47Speaker 1: It's doing exactly what we told it to do, even
- 13:49Speaker 1: if what we told it to do results in absolute lunacy. Precisely, so,
- 13:54Speaker 1: if memory itself is this fragile, what happens when lets
- 13:57Speaker 1: a more predictable human touch it, Like when a user
- 13:59Speaker 1: actually types something into the program using our input and
- 14:03Speaker 1: output functions, dealing.
- 14:04Speaker 2: With the outside world introduces entirely new vulnerabilities. The standard
- 14:08Speaker 2: C library gives us print for outputting text to the
- 14:10Speaker 2: screen and scam for taking input from the keyboard.
- 14:13Speaker 1: Printsif seems mostly harmless, though you can use scape sequences
- 14:17Speaker 1: to format text in weird ways. I was fascinated by
- 14:20Speaker 1: the carriage return sequence.
- 14:21Speaker 2: The backslash R no backslash R is a relic from
- 14:24Speaker 2: the typewriter days. It moves the invisible cursor back to
- 14:27Speaker 2: the absolute beginning of the current line, but critically it
- 14:30Speaker 2: does not move down to a new line.
- 14:32Speaker 1: Selekas and Silekas highlight a brilliant example of this. If
- 14:36Speaker 1: you tell print to print the word sample, then issue
- 14:39Speaker 1: the carriage return, and then print the word text, the
- 14:41Speaker 1: output on your screen isn't sample text.
- 14:44Speaker 2: No, it's not.
- 14:45Speaker 1: The output is the bizarre word text all because.
- 14:48Speaker 2: The machine prints sampl e. Then the carriage return snaps
- 14:53Speaker 2: the cursor back to hover over the S. Then it
- 14:55Speaker 2: twints text right over the top of the first four letters, overwriting.
- 14:59Speaker 1: Them writing the samps yes.
- 15:02Speaker 2: But it leaves the L and E sitting at the
- 15:04Speaker 2: end untouched textle.
- 15:05Speaker 1: Again malicious compliance, but printing text is relatively safe. Taking
- 15:10Speaker 1: user input is where the real danger lies. The scant function.
- 15:13Speaker 1: When I first tried to learn C, SCAMF was the
- 15:15Speaker 1: source of so many instant, mysterious program crashes.
- 15:19Speaker 2: It is arguably the most famous trap for novices. When
- 15:23Speaker 2: you use stamp to read a number from the keyboard
- 15:26Speaker 2: and store it in a variable, you must put an
- 15:28Speaker 2: ampersand the and symbol in front of the variable name
- 15:32Speaker 2: in your code.
- 15:33Speaker 1: So if my variable is named score, I can't just
- 15:35Speaker 1: write scamf and point it at score. I have to
- 15:37Speaker 1: write ampersand score exactly. If I forget that one tiny
- 15:41Speaker 1: little symbol, the whole program explodes. Why is SCAMF so
- 15:45Speaker 1: incredibly needy about the Ampersand when print chef doesn't require
- 15:49Speaker 1: it at all?
- 15:49Speaker 2: It goes back to our definition of variables as physical
- 15:52Speaker 2: memory addresses. When you use print, the function just needs
- 15:55Speaker 2: to look at the value currently stored in the variable
- 15:57Speaker 2: so it can display it.
- 15:59Speaker 1: It's like me handing you a photocopy of a document.
- 16:01Speaker 1: You can read it, you could show it to people,
- 16:03Speaker 1: but you aren't changing the original document sitting in my
- 16:05Speaker 1: filing cabinet exactly.
- 16:06Speaker 2: It's read only access. But SCAMF has a much more
- 16:10Speaker 2: invasive job Its entire purpose is to take new data
- 16:14Speaker 2: from the keyboard and save it into your variable. To
- 16:17Speaker 2: do that, it can't just look at a photocopy. It
- 16:19Speaker 2: needs to know the exact physical memory location in the
- 16:23Speaker 2: computer's RAM so it can go there and overwrite whatever
- 16:26Speaker 2: is currently in that cell.
- 16:27Speaker 1: So adding that ampersand is like handing scamp. The physical
- 16:30Speaker 1: GPS coordinates to the memory cell.
- 16:33Speaker 2: Yes, the ampersand is the address of operator. If you
- 16:37Speaker 2: forget it, SCAMF takes the current value of your variable.
- 16:40Speaker 2: Let's say the variable holds the number ten and assumes
- 16:42Speaker 2: that ten is the memory address.
- 16:44Speaker 1: Oh no.
- 16:45Speaker 2: It then takes the user's keyboard input and tries to
- 16:48Speaker 2: aggressively shove it into memory location ten. Memory location ten
- 16:52Speaker 2: almost certainly belongs to the operating system's core functions, and.
- 16:55Speaker 1: The operating system detects a rogue program trying to override
- 16:58Speaker 1: its brain and instill shuts you down a segmentation faull
- 17:02Speaker 1: all because of one missing amper.
- 17:04Speaker 2: Sand And remember, the compiler won't warn you. The compiler
- 17:08Speaker 2: sees STAMP pointing to a value, assumes you genuinely intended
- 17:12Speaker 2: to use that value as a raw memory address, and
- 17:14Speaker 2: compiles the time bomb directly into your software.
- 17:17Speaker 1: There is another SCAMF trap mentioned in the material that
- 17:19Speaker 1: doesn't crash the program, but effectively freezes it in time.
- 17:23Speaker 2: The hanging prompt trap. A lot of programmers intuitively add
- 17:27Speaker 2: a new line character a backslash end to the end
- 17:30Speaker 2: of their scam formatting string right they think it will
- 17:33Speaker 2: neatly move the cursor to the next line after the
- 17:36Speaker 2: user hits the enter key.
- 17:37Speaker 1: Because it's exactly what backslash end does in print. It's
- 17:40Speaker 1: just a habit.
- 17:41Speaker 2: But in SCAMF backslash end doesn't mean print a new line.
- 17:44Speaker 2: It tells the function to read and discard all white
- 17:47Speaker 2: space characters until it finds a physical, non white space character.
- 17:51Speaker 1: So the user types their number, hits enter, and uh,
- 17:55Speaker 1: nothing happens.
- 17:56Speaker 2: The program just sits there, completely frozen. You can hit
- 17:58Speaker 2: the enter key one thousand times. You are just generating
- 18:01Speaker 2: white space, which Stamp happily consumes in discards, waiting endlessly
- 18:05Speaker 2: until you type a letter or a number and press
- 18:07Speaker 2: enter again to break the loop.
- 18:09Speaker 1: So we've successfully stored our data, survived the memory traps,
- 18:13Speaker 1: and navigated the io mindfield without causing a segmentation fault,
- 18:16Speaker 1: barely barely. Yeah. Our final frontier of invisible traps lies
- 18:20Speaker 1: in how we manipulate that data mathematically.
- 18:23Speaker 2: C is legendary for its operator quirks, and perhaps the
- 18:26Speaker 2: most common mathematical error in all of programming is confusing
- 18:30Speaker 2: the assignment operator with the equality operator.
- 18:32Speaker 1: The single equals sign versus the double equal sign.
- 18:35Speaker 2: A single equals sign assigns of value A equals ten,
- 18:39Speaker 2: takes the number ten and puts it into the variable A.
- 18:42Speaker 2: A double equal sign compares two values. Okay, A double
- 18:45Speaker 2: equals ten. Asks the mathematical question, is the value currently
- 18:50Speaker 2: inside A exactly equal to ten?
- 18:52Speaker 1: So what happens if I am writing an if statement
- 18:54Speaker 1: try and ask a question? But I make a typo
- 18:57Speaker 1: instead of asking if a double equals ten, I accidentally
- 18:59Speaker 1: type if A equals ten with a single equal sign.
- 19:03Speaker 2: C will execute it perfectly. First, it evaluates the single
- 19:06Speaker 2: equal sign. It forcefully takes the number ten and assigns
- 19:09Speaker 2: it to the variable A, instantly destroying whatever data was
- 19:12Speaker 2: in A previously.
- 19:13Speaker 1: Oh wow.
- 19:13Speaker 2: Then it evaluates the result of that assignment to decide
- 19:16Speaker 2: if the if statement is true or false. In C,
- 19:18Speaker 2: any non zero value is considered true. Since ten is
- 19:22Speaker 2: not zero. The statement evaluates is true every single time.
- 19:25Speaker 1: Let me make sure I understand the gravity of that.
- 19:27Speaker 1: By missing one single keystroke, I have silently corrupted my
- 19:32Speaker 1: own data by overwriting my variable, and I have forced
- 19:36Speaker 1: a conditional branch of my program to always execute, regardless
- 19:41Speaker 1: of what the user actually did. Yep, and the compiler
- 19:43Speaker 1: just nods and says, looks good to me.
- 19:46Speaker 2: It assumes you are a genius who intended to perform
- 19:49Speaker 2: an assignment and an evaluation in the exact same.
- 19:52Speaker 1: Breath, which brings up the increment operators, the prefix and postfix.
- 19:56Speaker 1: This is another area where doing two things at once
- 19:58Speaker 1: creates a massive chronological trap.
- 20:00Speaker 2: You can increase a variable's value by exactly one by
- 20:03Speaker 2: typing two plus signs. If you put them before the
- 20:06Speaker 2: variable name, like plus plus a, it's called prefix. You
- 20:08Speaker 2: put them after like A plus plus, it's postfix.
- 20:11Speaker 1: Okay.
- 20:11Speaker 2: If they're just sitting on a line entirely by themselves,
- 20:13Speaker 2: they do the exact same thing. All goes up by one,
- 20:15Speaker 2: But the.
- 20:16Speaker 1: Trap springs when you use them inside a larger mathematical equation. Right,
- 20:20Speaker 1: the placement of those plus signs changes the actual timeline
- 20:24Speaker 1: of events.
- 20:25Speaker 2: Consider the equation B equals plus plus A. The prefix
- 20:29Speaker 2: means C increments the value of A before it evaluates
- 20:33Speaker 2: the rest of the equation. If A started as four,
- 20:35Speaker 2: it immediately becomes five, then that new value five is
- 20:39Speaker 2: assigned to B. Both variables end up holding five.
- 20:42Speaker 1: But if I use postfix, B equals A plus.
- 20:45Speaker 2: Plus postfix means C uses the current value of A
- 20:49Speaker 2: first and only increments it after the assignment is complete.
- 20:52Speaker 2: If a's four, it takes that four and assigns it
- 20:54Speaker 2: to B. The equation is over, and then then in
- 20:57Speaker 2: the background, it increments A to five. So B is
- 20:59Speaker 2: whole old data four while A has moved onto five.
- 21:02Speaker 1: Depending on which side of the letter you type, the
- 21:04Speaker 1: plus signs your entire program is either working with current
- 21:07Speaker 1: data or stale data. Exact, which actually segues beautifully into
- 21:11Speaker 1: our final and maybe most insidious chronological trap logical short circuiting. OH.
- 21:16Speaker 2: This raises an important question about side effects and code.
- 21:19Speaker 2: This involves the logical A and D operator, which is
- 21:21Speaker 2: two ampersands, and the logical R operator, which is two
- 21:24Speaker 2: pipe symbols.
- 21:25Speaker 1: Let's focus on the A and D operator. I want
- 21:28Speaker 1: to try an analogy here to see if I have the
- 21:29Speaker 1: mechanics right. Imagine a lazy bouncer working the door at
- 21:34Speaker 1: an exclusive nightclub.
- 21:35Speaker 2: Okay, I like it.
- 21:36Speaker 1: The rule to get in is strict. You must be
- 21:39Speaker 1: twenty one A and D you must be on the
- 21:41Speaker 1: guest list.
- 21:42Speaker 2: That is an A and D condition, a compound logical
- 21:44Speaker 2: expression requiring two true values.
- 21:47Speaker 1: Exactly. So, you walk up to the door. The bouncer
- 21:50Speaker 1: checks your ID first, he sees you're only eighteen. He
- 21:53Speaker 1: denies you entry immediately. Crucially, he does not even body
- 21:57Speaker 1: looking down at his clipboard to see if your name's
- 21:59Speaker 1: on the guest list. All right? Why Because the rule
- 22:02Speaker 1: requires both to be true. Since the first one is false,
- 22:05Speaker 1: the whole thing is mathematically false. Checking the list is
- 22:08Speaker 1: a waste of energy.
- 22:09Speaker 2: That is precisely how the c compiler evaluates the A
- 22:12Speaker 2: and D operator. It evaluates expressions from left to right.
- 22:15Speaker 2: If the left side is false, the compiler knows the
- 22:17Speaker 2: entire statement cannot possibly be true.
- 22:19Speaker 1: So it stops, just completely stops.
- 22:21Speaker 2: It completely ignores the right side of the equation.
- 22:23Speaker 1: A short circuit on the surface that sounds incredibly efficient.
- 22:26Speaker 1: It saves a computer from wasting processing cycles. Why is
- 22:30Speaker 1: it a trap?
- 22:30Speaker 2: Because of those side effects we mentioned earlier. Let's look
- 22:33Speaker 2: at an example from the Silicus and Silicus textbook. Imagine
- 22:36Speaker 2: this line of code C equals in parentheses a greater
- 22:40Speaker 2: than fifteen, A and D in parentheses plus b greater
- 22:44Speaker 2: than fifteen.
- 22:44Speaker 1: Let's trace that the variable C gets the result of
- 22:47Speaker 1: checking if A is greater than fifteen, A and D
- 22:50Speaker 1: if the incremented value of B is greater than fifteen.
- 22:53Speaker 2: Now suppose A is currently ten. The compiler looks at
- 22:57Speaker 2: the left side of the A and D operator. It's
- 22:59Speaker 2: ten fifteen false.
- 23:01Speaker 1: And because it's an A and D operation, the compiler
- 23:04Speaker 1: becomes the lazy bouncer. It's short circuits. It completely abandons
- 23:08Speaker 1: the right side of the equation.
- 23:09Speaker 2: But look at what was on the right side, A
- 23:10Speaker 2: prefix increment plus plus B. Because the right side was
- 23:14Speaker 2: entirely skipped to the variable B is never incremented.
- 23:17Speaker 1: Wow. If I wrote that line of code, my human
- 23:20Speaker 1: brain assumes B will go up by one every single
- 23:23Speaker 1: time that line is executed.
- 23:24Speaker 2: But B only increments if A happens to be greater
- 23:26Speaker 2: than fifteen. If A is ten, B just stays exactly
- 23:29Speaker 2: where it was. You have just introduced a.
- 23:31Speaker 1: Phantom bug that's terrifying.
- 23:33Speaker 2: Your data is now misaligned, and it might not actually
- 23:35Speaker 2: crash the program until thousands of lines later when a
- 23:38Speaker 2: completely different function relies on B being the correct number.
- 23:42Speaker 1: So what does this all mean? It is a language
- 23:45Speaker 1: that truly demands you think like a machine rather than
- 23:48Speaker 1: expecting machine to understand your human intent. From compiler illusions
- 23:52Speaker 1: and fractional rounding errors to the terrifying reality of negative
- 23:56Speaker 1: numbers magically becoming four point two billion.
- 24:00Speaker 2: It's all rooted in that original design philosophy from the
- 24:02Speaker 2: nineteen seventies. C achieves raw, unparalleled performance by delegating all safety,
- 24:08Speaker 2: all memory management, and all logical verification directly to you.
- 24:12Speaker 2: It rewards surgical precision, and it ruthlessly punishes assumptions, which leads.
- 24:18Speaker 1: Us with a rather profound reality to consider. Think about
- 24:20Speaker 1: the physical infrastructure of our modern society. C isn't just
- 24:24Speaker 1: a historical curiosity.
- 24:25Speaker 2: No, not at all.
- 24:26Speaker 1: It is the foundational language for the world's operating systems.
- 24:29Speaker 1: It runs the embedded processors in medical devices like pacemakers.
- 24:32Speaker 1: It controls the anti lock brakes in your car, and
- 24:34Speaker 1: the avionics navigating commercial airspace.
- 24:37Speaker 2: It is running anywhere that requires raw speed and immediate
- 24:41Speaker 2: unrestricted hardware control.
- 24:43Speaker 1: So, given everything we have explored today, given how terrifyingly
- 24:47Speaker 1: easy it is to make an invisible mistake to miss
- 24:50Speaker 1: a single amper sand, to accidentally type a single equal sign,
- 24:54Speaker 1: or to force a comparison between assigned and unsigned integer,
- 24:57Speaker 1: how many undiscovered ticking logic bombs are silently running in
- 25:01Speaker 1: the background of our modern world right now, just waiting
- 25:04Speaker 1: for the exact right conditions to trigger.
- 25:06Speaker 2: It's a sobering thought.
- 25:07Speaker 1: Next time you are walking through that perfectly manicured digital world,
- 25:10Speaker 1: just remember you are surrounded by tripwires. Thanks for joining
- 25:14Speaker 1: us on this deep dive,