5.1.30 · D3 · HinglishC Programming

Worked examplesUndefined behavior — comprehensive list, why to avoid

3,315 words15 min read↑ Read in English

5.1.30 · D3 · Coding › C Programming › Undefined behavior — comprehensive list, why to avoid

Yeh page UB topic note ka hands-on lab hai. Parent note ne categories list ki thin; yahan hum actual experiments chalate hain. Hum har tarah ke UB cases enumerate karte hain — har sign, har zero, har degenerate input, har limiting value — phir aise examples kaam karte hain jo us map ke har cell ko hit karte hain. Pehle tum guess karo ("Forecast"), phir hum step by step reason karte hain, phir hum real arithmetic se verify karte hain.

Pehli line se pehle: ek quick vocabulary anchor taaki koi bhi symbol samajh na aaye aisa na ho.


Scenario matrix

Practice mein milne wala har UB bug neeche diye gaye cells mein se kisi ek mein aata hai. Worked examples ka kaam hai ki har cell par kam se kam ek baar land kiya jaaye.

Cell Case class Degenerate / limiting input Example jo ise hit karta hai
A Signed overflow — positive side near INT_MAX Ex 1
B Signed overflow — the INT_MIN trap INT_MIN / -1 Ex 2
C Divide / modulo by zero denominator Ex 3
D Shift out of range shift or shift Ex 4
E Out-of-bounds — read past the end index length Ex 5
F Sequencing — modify twice, no order a[i] = i++ Ex 6
G Lifetime — dangling pointer pointer after scope ends Ex 7
H Varargs type mismatch (real-world) %d fed a double Ex 8
I Unsigned wrap is DEFINED (the control case) UINT_MAX + 1 Ex 9
J Exam twist — overflow check that vanishes x + 1 < x Ex 10

Prerequisite links yahan hain taaki jab bhi koi cell confuse kare tum neeche ja sako: Integer Types and Overflow, Sequence Points and Operator Precedence, Pointers and Memory in C, malloc and free — Dynamic Memory, Compiler Optimizations and -O flags, Sanitizers — ASan and UBSan, Strict Aliasing Rule.

Neeche di gayi figure map hai — ek signed int ki number line, jisme danger points mark kiye gaye hain. Har arithmetic example isi par refer karta hai.

Figure — Undefined behavior — comprehensive list, why to avoid

Dono taraf ke do red cliffs dekho: kisi bhi edge se bahar jaana UB hai. Beech ka mint region safe zone hai.


Cell A — Signed overflow, positive side


Cell B — The INT_MIN / -1 trap


Cell C — Divide / modulo by zero


Cell D — Shift out of range


Cell E — Out-of-bounds read


Cell F — Sequencing: modify twice, no order


Cell G — Lifetime: dangling pointer


Cell H — Varargs type mismatch (real-world)


Cell I — Control case: unsigned wrap is DEFINED


Cell J — Exam twist: woh check jo gayab ho jaata hai


Recall Har cell kaun sa tha?

Ex 1 :: Cell A — positive signed overflow Ex 2 :: Cell B — INT_MIN / -1 Ex 3 :: Cell C — divide/modulo by zero Ex 4 :: Cell D — shift out of range Ex 5 :: Cell E — out-of-bounds read Ex 6 :: Cell F — sequencing double-modify Ex 7 :: Cell G — dangling pointer lifetime Ex 8 :: Cell H — varargs %d vs double Ex 9 :: Cell I — unsigned wrap (defined control case) Ex 10 :: Cell J — the vanishing overflow check