🚀 Debugging Made Simple: How I Fixed My First Segmentation Fault in C
A beginner’s journey from panic to problem-solving in C programming.

🔹 Introduction
If you’ve ever written C programs, you’ve probably faced the dreaded Segmentation Fault (Core Dumped) error.
The first time I saw it, I froze. My program compiled fine, but when I ran it—boom 💥—it crashed instantly. I didn’t know what went wrong.
But instead of giving up, I decided to debug it step by step. In the process, I learned some powerful lessons about programming and problem-solving. Here’s my story and how you can fix segmentation faults too.
🔹 What is a Segmentation Fault?
A segmentation fault happens when your program tries to access memory it doesn’t own.
💡 Simple analogy:
Imagine memory as a row of houses 🏠. You’re allowed to enter your own house (allocated memory). If you suddenly try to break into your neighbor’s house (invalid memory), you’ll get caught. That’s exactly what the OS does—it throws a segmentation fault!
🔹 The Problem I Faced
Here’s a simplified version of my buggy code:
#include <stdio.h>
int main() {
int *ptr; // Declared but not initialized
*ptr = 10; // Trying to store value in garbage memory
printf("%d\n", *ptr);
return 0;
}
At first glance, it looks okay, right? But running this gave me:
Segmentation fault (core dumped)
🔹 How I Debugged It
Checked the error message → confirmed it was a memory access issue.
Added print statements → realized the pointer was never assigned valid memory.
Googled the error → found that uninitialized pointers often cause segmentation faults.
Tried using GDB debugger → it pointed to the exact line of crash.
🔹 The Fix
I corrected the code by assigning proper memory before using the pointer:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int)); // allocate memory
*ptr = 10; // safe to use now
printf("%d\n", *ptr);
free(ptr); // free allocated memory
return 0;
}
Now it runs perfectly ✅ and prints:
10
🔹 Lessons Learned
Always initialize variables and pointers before using them.
Learn to use debugging tools like GDB or even simple
printfstatements.Don’t panic when errors show up—they’re actually hints that guide you toward better code.
Every bug you fix makes you a stronger programmer 💪.
🔹 Conclusion
That segmentation fault, which once scared me, actually turned into a great learning opportunity. Debugging isn’t about being a genius—it’s about being patient and systematic.
So next time you see “Segmentation fault (core dumped)”, don’t worry. Take a deep breath, debug step by step, and remember—you’re not just fixing a bug, you’re training your brain to think like an engineer 🚀.