正在加载视频...

视频加载失败

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 次观看 • 2 个月前 •via X (Twitter)

31 条评论

Steven Cheng 的头像
Steven Cheng2 个月前

memcpy swap is classic. malloc overhead though.

Bab Dabbalina 的头像
Bab Dabbalina2 个月前

Why allocate memory? Use 3 xor operations

loam 的头像
loam2 个月前

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 的头像
caveman2 个月前

who is this lecturer? He’s great

Marek Knápek 的头像
Marek Knápek2 个月前

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

Dmt Elf ∞/21M 的头像
Dmt Elf ∞/21M2 个月前

Or you could use XOR 3 times

Enterprise Software Architecture 的头像
Enterprise Software Architecture2 个月前

👀 those were the good old days 🥹

Supreme King 的头像
Supreme King2 个月前

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 的头像
allOS.dev - Creating Cross-Platform Apps2 个月前

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

Tridiv 的头像
Tridiv2 个月前

Or the triple XOR

Grammar Sheriff 👮🏼‍♂️🚔 的头像
Grammar Sheriff 👮🏼‍♂️🚔2 个月前

This guy could be a fantastic vibe coder.

Caleb Maclennan 的头像
Caleb Maclennan2 个月前

Thanks for the lesson — in loving my Rust more!

geist 的头像
geist2 个月前

This is like crack

Marc Espie 的头像
Marc Espie2 个月前

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

Kim Nevelsteen, PhD 的头像
Kim Nevelsteen, PhD2 个月前

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 的头像
Per2 个月前

Should have used unsigned char instead of char?

Neko Witch Mawy 🪄 的头像
Neko Witch Mawy 🪄2 个月前

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 的头像
Pain2 个月前

raw low level

VB 的头像
VB2 个月前

Before vibe coding, people should understand that

loveiskind 的头像
loveiskind2 个月前

XOR ftw

MORTIFAGO NARANJA DEL GORDO DAN 🍆 的头像
MORTIFAGO NARANJA DEL GORDO DAN 🍆2 个月前

Any with extra steps

Steven Cheng 的头像
Steven Cheng2 个月前

Any alignment concerns with that byte buffer?

iuliu 的头像
iuliu2 个月前

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

Mythosgear 的头像
Mythosgear2 个月前

Void comment incoming

Haseeb Mir 的头像
Haseeb Mir2 个月前

I watched these in my college times great lectures

Yossi Levin 的头像
Yossi Levin2 个月前

Use alloca. Skip the free and gain performance.

nickel³ 的头像
nickel³2 个月前

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 的头像
Adel Bucetta2 个月前

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 🤖 的头像
Si_0perator 🤖2 个月前

🤔🤔

Pitchfork & Torch ♞ 的头像
Pitchfork & Torch ♞2 个月前

VoidSwap sounds like tinder for goth couples

matija 的头像
matija2 个月前

Brings me back

相关视频