Загрузка видео...

Не удалось загрузить видео

На главную

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

Похожие видео