5.1.30 · D1 · HinglishC Programming

FoundationsUndefined behavior — comprehensive list, why to avoid

2,181 words10 min read↑ Read in English

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

Yeh page har woh word aur symbol define karta hai jis par parent note ne rely kiya tha. Agar parent mein koi term achanak se aaya laga, toh woh yahan ground up se define kiya gaya hai. Upar se neeche padho — har idea ek brick hai jis par agla khada hota hai.


1. Program kya cheez hota hai? Bytes aur memory

Figure — Undefined behavior — comprehensive list, why to avoid

Topic ko yeh kyun chahiye: parent mein almost har UB ("out of bounds", "use-after-free", "dangling pointer") ek aisi mailbox ko touch karne ke baare mein hai jise tum touch karne ke allowed nahi the. "Out of bounds" ko samjhe bina mailboxes ki street aur tumhara fenced-off region kahan shuru aur khatam hota hai, yeh dekhe bina samajhna mushkil hai.


2. Variables aur unki lifetime

Figure — Undefined behavior — comprehensive list, why to avoid

Topic ko yeh kyun chahiye: parent ka Example 3 (return &local;) poori tarah lifetime ke baare mein hai. Boxes theek hain — number abhi bhi wahan baitha ho sakta hai — lekin woh ab local ke naam pe nahi hain. Unhe padhna UB hai kyunki lifetime khatam ho gayi, na ki memory gayab ho gayi.


3. Pointers — *, &, aur NULL ke symbols

Parent ke Category 1 ke saare examples pointers use karte hain, isliye ab har pointer symbol ko samajhte hain. Poore topic ke liye dekho Pointers and Memory in C.

Figure — Undefined behavior — comprehensive list, why to avoid

Yeh dono kyun hain aur kuch aur kyun nahi: & se tum ek pointer banate ho (tumhe store karne ke liye house number chahiye), aur * se tum use karte ho (house number tab tak bekar hai jab tak tum actually wahan walk nahi karte). Parent ka phrase "dereferencing a null pointer" literally matlab hai: *p karna jab p ek forbidden address rakhta ho.


4. Arrays aur a[i] ka matlab

Figure — Undefined behavior — comprehensive list, why to avoid

Topic ko yeh kyun chahiye: parent ka Example 4 size-3 array se a[3] padhta hai. Figure dekho — index fence ke bahar ek aisi box mein step karta hai jo kisi aur ki hai. Woh step out of bounds hai, aur ise padhna ya likhna UB hai. Valid range hai se ; "one past the end" wali box ko point kiya ja sakta hai lekin khola nahi ja sakta.


5. Integers, bits, aur overflow

Parent ki Category 2 (arithmetic UB) ke liye tumhe pata hona chahiye ki numbers kaise store hote hain. Poora topic: Integer Types and Overflow.

Figure — Undefined behavior — comprehensive list, why to avoid

Topic ko yeh kyun chahiye: parent ka Example 1 ek overflow check delete karta hai kyunki signed overflow UB hai. Compiler assume karne ka haqdar hai ki "yeh kabhi nahi hota". Agar overflow sirf "wrap-around" hota (jaise unsigned mein hota hai), toh check survive karta. Poora example is asymmetry par tika hua hai.


6. Sequence points — kab ek change "count" hota hai?

Category 3 UB (i = i++) ke liye ek aur idea chahiye. Dekho Sequence Points and Operator Precedence.


7. Compiler ek reasoner ke roop mein (woh piece jo UB ko scary banata hai)

Topic ko yeh kyun chahiye: yeh ek rule hi woh reason hai ki UB "poore program ko poison karta hai" (parent ka mistake #2). Compiler ek impossible-by-assumption path se backwards aur forwards reason karta hai, isliye line 50 par UB line 10 par ek check silently erase kar sakta hai. Parent ke har worked example ka yeh ek line consequence hai.


Prerequisite map

Bytes and memory street

Variables and lifetime

Pointers: address-of and star

Arrays and index a of i

Integers width signed unsigned

Overflow signed vs unsigned

Side effects and sequence points

Sequencing UB

Compiler and optimizer assumption

Undefined Behavior topic

Yeh parent ko feed karta hai: the UB comprehensive list.


Equipment checklist

Right side cover karo aur aage badhne se pehle khud ko test karo.

Byte kya hoti hai aur woh kaunsi values range rakhti hai?
Ek single 8-bit box jo 0 se 255 tak ek number rakhti hai, kyunki patterns hote hain.
Address kya hota hai (us address par value ke mukable mein)?
Ek house number jo ek mailbox identify karta hai — uski location, contents nahi.
&x ka plain words mein kya matlab hai?
"x ka address" — mujhe woh house number do jahan x rehta hai.
*p (dereference) kya karta hai?
p mein stored address par jaao aur woh mailbox kholo — woh cheez padho/likho jis par p point kar raha hai.
Ek automatic (local) variable ki lifetime kya hoti hai?
Jab se function use karna shuru karta hai tab se jab tak function return karta hai; phir uske boxes reclaim ho jaate hain.
Ek dangling pointer dangerous kyun hai, bhaley hi woh ek number rakhta ho?
Woh number ek purana house address hai jiske owner ki lifetime khatam ho gayi; woh box ab kisi ka bhi data rakh sakta hai.
int a[3] ke liye padhne ke liye kaunse indices valid hain?
Sirf 0, 1, 2 (yaani 0 se n-1 tak); a[3] out of bounds aur UB hai.
Kaun sa overflow defined hai aur kaun sa UB hai?
Unsigned overflow defined hai (wraps mod ); signed overflow UB hai.
Sequence point kya hota hai?
Ek checkpoint jahan saare pehle ke side effects baad wale shuru hone se pehle khatam ho jaane chahiye.
UB ke baare mein optimizer ki core assumption batao.
Woh assume karta hai ki UB kabhi nahi hota, isliye koi bhi path jo UB hoga use unreachable treat kiya jaata hai aur delete kiya ja sakta hai.