YOUR C LEARNING COMPANION

Kahan se padhein?

Learning path
Sahi aur reliable CChapter 66 of 73

Diagnostics, debugging aur evidence

Reproducible case build, right tools se investigate, detector limits aur contract-focused regression checks samjhein.

5 min readIntermediateC17 baseline
Offline reading
Is chapter mein8 sections

Debugging expected aur observed behavior ke mismatch se shuru hoti hai. Useful investigation failure repeatable banati hai, specific explanation sochti hai aur evidence check karti hai. Tools help karte hain, lekin har tool problem ka kuch hissa dekhta hai.

1. Observation reproducible banayein

Input, compiler/version, flags, target, relevant libraries aur expected/observed output record karein. Failure preserve karke case reduce karein. Call remove, type change ya optimization disable karna lifetimes, ordering ya investigated UB ko change kar sakta hai. Smaller non-reproducing program useful comparison evidence hai, automatically repair proof nahi.

2. Checked aur inspectable build se begin karein

Complete C17 program · diagnose.c
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    const int values[] = { 2, 4, 6 };
    size_t count = sizeof values / sizeof values[0];
    int sum = 0;
    for (size_t i = 0; i < count; ++i) sum += values[i];
    assert(sum == 12);
    if (printf("sum=%d\n", sum) < 0) return EXIT_FAILURE;
    return fflush(stdout) == EOF ? EXIT_FAILURE : EXIT_SUCCESS;
}
GCC / GNU tools · example commands
gcc -std=c17 -Wall -Wextra -Wpedantic -Wconversion -Wformat=2 -g -O0 diagnose.c -o diagnose
./diagnose

Complete source diagnose.c mein save aur suitable Unix-like environment mein example GCC commands run karein. -g debug information emit karta hai; initial source-oriented inspection mein -O0 useful hai. Har target par identical debugger behavior promised nahi. Configuration-sensitive problem investigate karte waqt actual production optimization/configuration bhi reproduce karein.

Warnings suspicious conversions, formats, declarations aur control flow expose karti hain. -Wall named set hai, every possible warning nahi; -Wextra aur -Wconversion other checks add karte hain. -Werror enabled diagnostics ki build policy hai, correctness proof nahi. Suppress karne se pehle cause investigate aur justified narrow exception document karein.

3. Debugger se hypothesis inspect karein

Breakpoint selected location/condition par stop; watchpoint relevant expression change par stop kar sakta hai. Symptom se pehle indices, lengths, lifetime aur path inspect karein. Debugger display us build ka evidence hai, new language guarantee nahi. Optimized code operations reorder/remove aur variables unavailable report kar sakta hai. Logging scheduling/timing perturb bhi kar sakti hai.

4. Detector question ke according choose karein

Tool or buildEvidence aur limit
AddressSanitizerMany invalid bounds/lifetime accesses; har error ya general uninitialized-value detector nahi.
UndefinedBehaviorSanitizerSigned overflow/alignment jaise selected UB checks; coverage incomplete.
ThreadSanitizerExercised executions par race evidence; all schedules/algorithm correctness proof nahi.
Memory analysisUninitialized-value/leak coverage tool aur host support par depend.
Static analysisRun kiye bina paths examine; false positives aur missed defects possible.

Supporting GCC target par -fsanitize=address,undefined first two instrumentations select karta hai; debug information aur frame pointers reports improve kar sakte hain. Separate -fsanitize=thread build different question poochta hai. GCC ThreadSanitizer ko AddressSanitizer/LeakSanitizer ke saath combine allow nahi karta. Compiler flag accept kare tab bhi runtime availability separate hai. First invalid operation ki report often last crash se zyada useful hai.

5. Repair ko boundary-focused regression check banayein

Complete C17 program
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>

static bool last_value(const int *values, size_t count, int *result)
{
    if (values == NULL || result == NULL || count == 0) return false;
    *result = values[count - 1];
    return true;
}

int main(void)
{
    int values[] = { 2, 4, 6 };
    int result = 99;
    bool empty = last_value(values, 0, &result);
    assert(!empty && result == 99);
    bool one = last_value(values, 1, &result);
    assert(one && result == 2);
    bool many = last_value(values, 3, &result);
    assert(many && result == 6);
    return 0;
}

Helper one subtract karne se pehle zero count reject karta hai. Checks rejected result preserve aur empty, one-element, multiple-element cases cover karti hain. Caller count readable elements provide karega. Failure explain karne wala contract boundary test karein, implementation-string matching nahi. Passing suite uncertainty narrow karti hai; review/preconditions ab bhi matter.

Primary references aur aage ki reading

Aage badhne se pehle

  • Useful bug report failure reproduce karwati hai aur expected behavior batati hai.
  • Diagnostics, debugger observations aur sanitizers different questions ke answers dete hain.
  • Regression check failure wali boundary ya assumption exercise kare.
In ideas ko alag rakhein
No report / Proof of correctness
Tool silence uski coverage, instrumentation, inputs aur detect karne layak properties tak limited hai.
Apne words mein samjhaayein

Compiler options ya input guess kiye bina doosre ko failure investigate karne ke liye kya evidence denge?

Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.

Apni understanding check karein

Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.

Clean sanitizer run se kya establish hota hai?
Har choice ki explanation padhein
  1. Exercised executions mein covered problem report nahi hui.

    ✓ Sahi reasoning. Yeh useful evidence hai, par har input, path ya tool limitation ke liye proof nahi.

  2. Har possible execution valid C hai.

    Finite test run har possible execution explore nahi karta.

  3. Implementation ko ab language rules ki need nahi.

    Tools language rules ko support karte hain; replace nahi.

General

Earlier edition ka quick reference

Existing section links aur saved passages ke liye retained. Connected ideas expanded lessons mein hain; exact corrections source review mein recorded hain.

Kyunki bahut saari C errors ko standard sirf "undefined behavior" ke roop mein hi define karta hai, compile time par pakadta nahi, tooling reliable C likhne mein ek bahut bada role play karti hai:

  • Compiler warnings. GCC/Clang ka useful starting point -std=c17 -Wall -Wextra -Wpedantic -Wconversion -Wformat=2 hai. -Wall ka matlab har possible warning nahi aur -Wall -Wextra saari conversion warnings enable nahi karta. Policy appropriate ho toh -Werror enabled warnings ko errors banata hai. Diagnostics kai mistakes pakad sakti hain, lekin program mein undefined behavior na hone ka proof nahi.
  • assert. Un conditions ke liye sasti, built-in runtime checks jo hamesha true honi chahiye (Section 26).
  • Debuggers. gdb jaise tools programmer ko breakpoints set karne, execution ko line-by-line step karne, aur variables aur memory inspect karne dete hain — ek crash ki exact wajah samajhne ke liye indispensable.
  • Memory aur runtime tools. Valgrind Memcheck invalid memory accesses, kuch uninitialized-value uses aur leaks report kar sakta hai. AddressSanitizer (-fsanitize=address) kai bounds/lifetime errors detect karta hai; UndefinedBehaviorSanitizer (-fsanitize=undefined) signed overflow jaise selected undefined behaviors detect karta hai. Coverage alag hai; koi bhi har error nahi pakadta aur ordinary AddressSanitizer general uninitialized-memory detector nahi. Availability aur leak detection platform par depend karti hain.
  • Static analysis. clang-tidy aur cppcheck jaise tools source code ko bina run kiye examine karte hain, bugs ki poori classes pakadte hain — aur, stricter industrial settings mein, standards-conformance issues bhi — is se pehle ki program kabhi execute ho.

Agla idea padhne ke liye tayyar?

Mark this chapter as read whenever you feel ready.

CHAPTER 66 / 73

Is chapter mein

Diagnostics, debugging aur evidence

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping