पाठ 37 / 38
डिबगिंग के मूल तत्व
printf, compiler warnings और gdb breakpoints व stepping से bugs खोजें।
printf डिबगिंग
सबसे सरल तकनीक: वेरिएबल मान छापने और यह पुष्टि करने के लिए कि कौन-सी लाइनें असल में चलती हैं, printf कॉल जगह-जगह लगाएँ। तेज़, पर हर बार एडिट और रीकंपाइल करना पड़ता है।
डिबग सिंबल के साथ कंपाइल करें
-g वह डिबग जानकारी शामिल करता है जो gdb को चाहिए; -Wall -Wextra चलाने से पहले ही संदिग्ध कोड सामने ला देता है।
gcc -g -Wall -Wextra prog.c -o prog
gdb ./progएक न्यूनतम gdb सत्र
gdb के अंदर: break main ब्रेकपॉइंट सेट करता है, run प्रोग्राम शुरू करता है, next लाइन दर लाइन आगे बढ़ता है, और print x किसी वेरिएबल का मौजूदा मान दिखाता है।
अंतिम क्विज़ 1/8
अंतिम क्विज़
त्वरित जाँच: gcc hello.c -o hello क्या करता है?
- Runs the program
- Deletes hello.c
- Compiles the source into an executable
- Installs gcc
Answer
Compiles the source into an executable — -o output file को नाम देता है।
अंतिम क्विज़ 2/8
अंतिम क्विज़
त्वरित जाँच: ints के साथ C में 7 / 2 क्या है?
- 3.5
- 4
- 2
- 3
Answer
3 — Integer division दशमलव हटा देता है।
अंतिम क्विज़ 3/8
अंतिम क्विज़
त्वरित जाँच: scanf("%s") की जगह fgets क्यों उपयोग करें?
- It limits input to the buffer size
- It is shorter
- It parses numbers
- It ignores spaces
Answer
It limits input to the buffer size — scanf %s में bounds checking नहीं है।
अंतिम क्विज़ 4/8
अंतिम क्विज़
त्वरित जाँच: p pointer हो तो *p क्या करता है?
- Gets its address
- Accesses the value it points to
- Frees it
- Declares it
Answer
Accesses the value it points to — * pointer को dereference करता है।
अंतिम क्विज़ 5/8
अंतिम क्विज़
त्वरित जाँच: swap(int x, int y) caller के variables क्यों नहीं बदल पाता?
- C forbids swapping
- The compiler removes it
- Arguments are copied
- It needs a header
Answer
Arguments are copied — मूल बदलने के लिए pointers पास करें।
अंतिम क्विज़ 6/8
अंतिम क्विज़
त्वरित जाँच: कौन-सा function heap पर memory allocate करता है?
- printf
- strlen
- fopen
- malloc
Answer
malloc — हर malloc को एक free से जोड़ें।
अंतिम क्विज़ 7/8
अंतिम क्विज़
त्वरित जाँच: free() के तुरंत बाद pointer का क्या करें?
- Set it to NULL
- Free it again
- Use it
- Cast it to int
Answer
Set it to NULL — इससे dangling-pointer उपयोग से बचाव होता है।
अंतिम क्विज़ 8/8
अंतिम क्विज़
त्वरित जाँच: Runtime पर buffer overflows कौन-सा tool पकड़ सकता है?
- make clean
- AddressSanitizer
- ls
- diff
Answer
AddressSanitizer — -fsanitize=address के साथ compile करें।