🎓 Built for 3rd Sem CS Lab Survival

Stop Dreading Segmentation fault (core dumped)

DebugPal doesn't just hand you the answer. It draws the exact Stack & Heap memory layout, asks Socratic questions to help you spot your pointer bug, and explains low-level concepts with real-world analogies.

Source Code main.c
Lab Presets:
1
2
3
4
5
Bug Classification NULL Pointer Dereference
Crash Point Line 18
Memory Health Critical Invariant Violation
🥞 Call Stack (Local Frames) High Address: 0x7ffd98a0
📦 Heap (Dynamic Allocations) Low Address: 0x00501000
🎯 Pointer Execution Timeline (What Happened Right Before the Crash):

🤔 Socratic Invariant Discovery

Don't look at the solution immediately! Step through these 3 guided questions to understand why this bug occurs so you'll never make it again on an exam.

Step 1: The Observation
Step 2: The Invariant Check
Step 3: The Lightbulb Reveal
💡 Real-World Mental Model

The Airbnb Key After Checkout

🗝️ ➡️ 🚪❌

Imagine renting an Airbnb for the weekend. On Monday morning, you checkout and the host cleans the room. However, you secretly made a copy of the front door key. On Tuesday night, you try to unlock the apartment door with your old key and walk in to sleep on the bed.

Why this relates to C pointers:

In C, free(ptr) returns ownership of the memory back to the operating system, but the variable ptr still holds the old address! Dereferencing a freed pointer is like trespassing with an old key — it may crash immediately (segfault), or worse, silently corrupt whatever data now lives there.

🛡️ Defensive Code Patch & Best Practice

Compare the vulnerable code against the memory-safe implementation:

❌ Vulnerable (Crashes / Leaks)
✅ Safe (Guarded & Cleaned)

Rule of Thumb Checklist to Avoid This:

  • Always check if pointer is non-NULL before dereferencing: if (ptr != NULL)
  • Immediately set pointer to NULL after freeing: free(ptr); ptr = NULL;
  • Pair every malloc() with an explicit free() along every return path.