Loading video...

Video Failed to Load

Go Home

To build a generic swap in C, pass the size explicitly, create a temporary byte buffer, then use memcpy three times to rotate the bit patterns. VLA char buffer[size] is a GCC thing, for a more portable version malloc the temp and free it. Stanford CS107, lecture 4

46,456 views • 2 months ago •via X (Twitter)

31 Comments

Steven Cheng's profile picture
Steven Cheng2 months ago

memcpy swap is classic. malloc overhead though.

Bab Dabbalina's profile picture
Bab Dabbalina2 months ago

Why allocate memory? Use 3 xor operations

loam's profile picture
loam2 months ago

The size argument should have size_t type as it does in memcpy. If you use a sized int, you're limiting the range of the size argument and you arguably need to check for negatives.

caveman's profile picture
caveman2 months ago

who is this lecturer? He’s great

Marek Knápek's profile picture
Marek Knápek2 months ago

Yeah, this code uses VLA, a feature that is frowned upon.

Dmt Elf ∞/21M's profile picture
Dmt Elf ∞/21M2 months ago

Or you could use XOR 3 times

Enterprise Software Architecture's profile picture
Enterprise Software Architecture2 months ago

👀 those were the good old days 🥹

Supreme King's profile picture
Supreme King2 months ago

This doesn't prevent using different types of pointers, right? So it's a clever way of allowing users to attempt to overwrite data 🤔

allOS.dev - Creating Cross-Platform Apps's profile picture
allOS.dev - Creating Cross-Platform Apps2 months ago

I watched the entire 6 minutes at normal speed! Really interesting. Covers a lot of topics in a great way.

Tridiv's profile picture
Tridiv2 months ago

Or the triple XOR

Grammar Sheriff 👮🏼‍♂️🚔's profile picture
Grammar Sheriff 👮🏼‍♂️🚔2 months ago

This guy could be a fantastic vibe coder.

Caleb Maclennan's profile picture
Caleb Maclennan2 months ago

Thanks for the lesson — in loving my Rust more!

geist's profile picture
geist2 months ago

This is like crack

Marc Espie's profile picture
Marc Espie2 months ago

Vp1/vp2 are the stupidest variable names I've seen in a long time.

Kim Nevelsteen, PhD's profile picture
Kim Nevelsteen, PhD2 months ago

If nothing else, I would typedef the char, but it is just ugly imo to use a char buffer for data that you don’t know to be characters.

Per's profile picture
Per2 months ago

Should have used unsigned char instead of char?

Neko Witch Mawy 🪄's profile picture
Neko Witch Mawy 🪄2 months ago

VLA blows up if size > stack, and stack is surprisingly tiny. But not using VLA is trivial here. Even then, why bother with all that? For register-sized swaps, it's much cheaper to do inline, that likely avoids memory access and/or results in a noop. For big swaps, no need to copy data around, it is simply enough to swap pointers around one level up. You know, kinda the point of pointers is to abstract data location.

Pain's profile picture
Pain2 months ago

raw low level

VB's profile picture
VB2 months ago

Before vibe coding, people should understand that

loveiskind's profile picture
loveiskind2 months ago

XOR ftw

MORTIFAGO NARANJA DEL GORDO DAN 🍆's profile picture
MORTIFAGO NARANJA DEL GORDO DAN 🍆2 months ago

Any with extra steps

Steven Cheng's profile picture
Steven Cheng2 months ago

Any alignment concerns with that byte buffer?

iuliu's profile picture
iuliu2 months ago

Can't we just use Claude Code to do this instead?

Mythosgear's profile picture
Mythosgear2 months ago

Void comment incoming

Haseeb Mir's profile picture
Haseeb Mir2 months ago

I watched these in my college times great lectures

Yossi Levin's profile picture
Yossi Levin2 months ago

Use alloca. Skip the free and gain performance.

nickel³'s profile picture
nickel³2 months ago

I have to admit that I didn’t go to college for CS it’s self taught but isn’t this like lesson 1? Like I am slightly concerned that people find this to be new information

Adel Bucetta's profile picture
Adel Bucetta2 months ago

that's exactly how i've been doing swaps in my own projects but i think it's worth noting that with clang you don't need to use memcpy at all, just a simple pointer arithmetic will do

Si_0perator 🤖's profile picture
Si_0perator 🤖2 months ago

🤔🤔

Pitchfork & Torch ♞'s profile picture
Pitchfork & Torch ♞2 months ago

VoidSwap sounds like tinder for goth couples

matija's profile picture
matija2 months ago

Brings me back

Related Videos