5.1.30 · HinglishC Programming

Undefined behavior — comprehensive list, why to avoid

2,234 words10 min readRead in English

5.1.30 · Coding › C Programming


WHAT, WHY, HOW


Comprehensive List (UB ka 80/20 jo actually milti hai)

Figure — Undefined behavior — comprehensive list, why to avoid

Worked Examples — saath mein "Yeh step kyun?"


Steel-man the Mistakes


Isse AVOID kaise karein (actionable 20%)


Active Recall

C mein Undefined Behavior (UB) kya hai?
Aisi situation jis par standard KOI requirement impose nahi karta; poore program ka behavior void ho jaata hai — kuch bhi ho sakta hai.
UB vs unspecified vs implementation-defined?
UB = bilkul koi requirement nahi; unspecified = kai allowed outcomes mein se ek, undocumented; implementation-defined = allowed outcome jo DOCUMENT hona CHAHIYE.
Kya signed integer overflow UB hai?
Haan — INT_MAX + 1 undefined hai.
Kya unsigned integer overflow UB hai?
Nahi — yeh 2^n modulo wrap karne ke liye defined hai.
Compiler UB ko exploit kyun karta hai?
Yeh assume karta hai ki UB kabhi nahi hoti, jo aggressive optimizations ko license deta hai (e.g. "impossible" checks delete karna).
i = i++; UB kyun hai?
i do baar modify hota hai bina kisi intervening sequence point ke jo dono modifications ko order kare.
Kya one-past-the-end pointer dereference allowed hai?
Nahi — pointer form karna legal hai, isse dereference karna UB hai.
"Yeh sahi run hua" proof kyun nahi hai ki code UB-free hai?
UB appear kar sakta hai kaam karne ka, phir naye compiler, flag, ya input se break ho jaaye; yeh spec se nondeterministic hai.
UB pakadne ke liye do compiler tools?
-fsanitize=undefined (UBSan) aur -fsanitize=address (ASan), plus -Wall -Wextra.
100 add karne se pehle signed overflow check karne ka sahi tarika?
if (x > INT_MAX - 100) ... — non-overflowing domain mein compare karo.
Kya string literal modify karna UB hai?
Haan — char *s="hi"; s[0]='H'; undefined hai.
Dangling pointer kya hota hai?
Aisi storage ki taraf pointer jiska lifetime khatam ho gayi hai (freed memory ya returned local).
Recall Feynman: 12-saal ke bachche ko samjhao

Ek board game socho jisme ek rulebook hai. Zyaadatar rules exactly batate hain kya karna hai. Lekin ek scary rule hai: "Agar tumne kabhi YAHI forbidden move ki, toh rulebook apply hona band ho jaata hai aur game literally kuch bhi kar sakta hai — tumhe million points de, board flip kare, ya game crash kare." C mein, woh forbidden moves undefined behavior hain. Tricky part yeh hai: computer ko assume karne ki permission hai ki tum kabhi forbidden move nahi karoge, isliye woh safety checks skip karta hai taaki faster chale. Jab tum obey karte ho tab yeh achha hai, lekin agar tum ghalti karo, toh pehle ki moves bhi galat ho sakti hain. Toh lesson simple hai: kabhi forbidden moves mat karo — list ke end se past mat padho, kisi cheez ko throw karne ke baad use mat karo, number ko itna bada mat banao ki fit na ho.

Connections

Concept Map

defines clause

allows set

must document

danger rank higher than

danger rank higher than

enables

grants

deletes code

voids meaning of

category 1

category 2

examples

examples

C Standard Contract

Undefined Behavior

Unspecified Behavior

Implementation-defined

Performance and Portability

Optimizer assumes UB never happens

Bugs upstream of the line

Entire Program

Memory and Pointer errors

Arithmetic errors

null deref, use-after-free, out of bounds

signed overflow, div by zero, bad shift