Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

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 Aufrufe • vor 2 Monaten •via X (Twitter)

30 Kommentare

Profilbild von TempeTeaParty
TempeTeaPartyvor 2 Monaten

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?

Profilbild von FM
FMvor 2 Monaten

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

Profilbild von Max Sanna
Max Sannavor 2 Monaten

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

Profilbild von Ali 🦁
Ali 🦁vor 2 Monaten

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

Profilbild von jhonny
jhonnyvor 2 Monaten

Use Claude or GPT sol or Grok

Profilbild von proc0
proc0vor 2 Monaten

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

Profilbild von Dark Goldenrod
Dark Goldenrodvor 2 Monaten

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.

Profilbild von Zavian
Zavianvor 2 Monaten

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

Profilbild von Benathon
Benathonvor 2 Monaten

I would never do that

Profilbild von Mickey Stewart
Mickey Stewartvor 2 Monaten

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.

Profilbild von Advanced SuperSilly Intelligence
Advanced SuperSilly Intelligencevor 2 Monaten

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

Profilbild von ashrealite
ashrealitevor 2 Monaten

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

Profilbild von ∂≈∞ --- ∫∫∫∆∏∑∫∫∫ π⃜÷⃜×⃜•⃜|⃜¶⃜∆⃜$⃜%⃜|⃜+⃜
∂≈∞ --- ∫∫∫∆∏∑∫∫∫ π⃜÷⃜×⃜•⃜|⃜¶⃜∆⃜$⃜%⃜|⃜+⃜vor 2 Monaten

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

Profilbild von mutex.up
mutex.upvor 2 Monaten

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.

Profilbild von Matcha
Matchavor 2 Monaten

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).

Profilbild von Augustus
Augustusvor 2 Monaten

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

Profilbild von smyanthota
smyanthotavor 2 Monaten

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

Profilbild von Abbas Banu Vohra
Abbas Banu Vohravor 2 Monaten

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

Profilbild von Unique.Aioli8518
Unique.Aioli8518vor 2 Monaten

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

Profilbild von Youssef Afella
Youssef Afellavor 2 Monaten

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

Profilbild von zaphodicus
zaphodicusvor 2 Monaten

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

Profilbild von ElectraX
ElectraXvor 2 Monaten

That's not a bug.

Profilbild von 泰多宝
泰多宝vor 2 Monaten

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

Profilbild von gb
gbvor 2 Monaten

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.

Profilbild von Matthew
Matthewvor 2 Monaten

Ibit++ known zeros

Profilbild von kimiyoribaka
kimiyoribakavor 2 Monaten

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

Profilbild von Wade Noam Sayen
Wade Noam Sayenvor 2 Monaten

These curated into a collection is massive win

Profilbild von Orion Night
Orion Nightvor 2 Monaten

every C programmer learns this exactly once

Profilbild von Path of Totality
Path of Totalityvor 2 Monaten

Maybe our preprocessors need to be a little smarter 😆

Profilbild von Steve Mac
Steve Macvor 2 Monaten

I love these little clips!

Ähnliche Videos