正在加载视频...

视频加载失败

This is a common C bug. You write a MAX macro, then call MAX(m++, n++). The preprocessor pastes the text, so it becomes (m++ > n++) ? m++ : n++. The smaller one increments once. The larger one increments twice. Stanford CS107, lecture 12

29,707 次观看 • 2 个月前 •via X (Twitter)

30 条评论

TempeTeaParty 的头像
TempeTeaParty2 个月前

This isn't so much a coding bug as it is a comprehension problem on the part of the programmer. How can this be common?

FM 的头像
FM2 个月前

It's interesting that the `inline` keyword is still not standard in C. It could've saved a lot of headache.

Max Sanna 的头像
Max Sanna2 个月前

I would never dream, not even by mistake, of using this macro in this way

Ali 🦁 的头像
Ali 🦁2 个月前

This isn't a 🐞 this is expected preprocessor behavior! Some compilers might resolve the issue though

jhonny 的头像
jhonny2 个月前

Use Claude or GPT sol or Grok

proc0 的头像
proc02 个月前

You don't use ++ more than once in the same statement, it's undefined behavior.

Dark Goldenrod 的头像
Dark Goldenrod2 个月前

I almost never use inline ++ or --. Yeah, it is an entire 3 seconds faster to type: arr[ idx++ ] = item than: arr[ idx ] = item; idx++; but it's slower to read every time.

Zavian 的头像
Zavian2 个月前

Become a teacher become a dev solana:5yC9BM8KUsJTPbWPLfA2N8qH1s9V8DQ3Vcw1G6Jdpump, become a ai video creator, what moore?

Benathon 的头像
Benathon2 个月前

I would never do that

Mickey Stewart 的头像
Mickey Stewart2 个月前

I remember watching this Stanford cs class on iTunes U when all these universities first started to make these available. These were awesome and So was Harvards CS50.

Advanced SuperSilly Intelligence 的头像
Advanced SuperSilly Intelligence2 个月前

The human suffering needs to end. Thank goodness for AI.

ashrealite 的头像
ashrealite2 个月前

I cant believe we whiteboard dimwitted shit like this in supposed prestigious universities.

∂≈∞ --- ∫∫∫∆∏∑∫∫∫ π⃜÷⃜×⃜•⃜|⃜¶⃜∆⃜$⃜%⃜|⃜+⃜ 的头像
∂≈∞ --- ∫∫∫∆∏∑∫∫∫ π⃜÷⃜×⃜•⃜|⃜¶⃜∆⃜$⃜%⃜|⃜+⃜2 个月前

Master tetsuo, your posts brighten my day. I am grateful that I discovered your account.

mutex.up 的头像
mutex.up2 个月前

That's not a C bug, that's simply correct text substitution by the preprocessor. Use the -E flag to check the preprocessed code if unsure. "inline" functions are safer, and "const" constants can be seen by a debugger. Understand your tools.

Matcha 的头像
Matcha2 个月前

I guess this is why I'm killing macros and replacing with __inline__ functions. AS I UNderstand it, the __inline__ functions expand to code within the calling location just like a macro, but without all the macro gotchas (like this one).

Augustus 的头像
Augustus2 个月前

Today, you need a really good reason to use defines. Inline functions > defines

smyanthota 的头像
smyanthota2 个月前

gcc statement expressions with typeof kill the double-eval, or just use an inline function and move on

Abbas Banu Vohra 的头像
Abbas Banu Vohra2 个月前

thanks thats why i tRUST because it (its designers) know that increment/decrement ops are pain in the ... : abv_bay_yanlis_8647_DMGINXL

Unique.Aioli8518 的头像
Unique.Aioli85182 个月前

It's a feature, not a bug (at least not a C bug, anyway)!

Youssef Afella 的头像
Youssef Afella2 个月前

Everyone think using a chair is safe but watch what happens when you beat the shit of someone with it

zaphodicus 的头像
zaphodicus2 个月前

It is not a bug but a programmer mistake. A function is much better and the compiler will inline it automatically is needed. The macro system, even if I love it, is full of traps

ElectraX 的头像
ElectraX2 个月前

That's not a bug.

泰多宝 的头像
泰多宝2 个月前

the gcc statement-expression fix assigns each argument to a __typeof__ temp first, so the side effect happens exactly once.

gb 的头像
gb2 个月前

Except it's not that common... unless you're a beginner and don't know it's a macro (despite it being written in capitals) ofc.

Matthew 的头像
Matthew2 个月前

Ibit++ known zeros

kimiyoribaka 的头像
kimiyoribaka2 个月前

I don't like mid-expression incrementing to begin with, but doing multiple at once seems like it's really asking for trouble.

Wade Noam Sayen 的头像
Wade Noam Sayen2 个月前

These curated into a collection is massive win

Orion Night 的头像
Orion Night2 个月前

every C programmer learns this exactly once

Path of Totality 的头像
Path of Totality2 个月前

Maybe our preprocessors need to be a little smarter 😆

Steve Mac 的头像
Steve Mac2 个月前

I love these little clips!

相关视频