I hit compile with gcc and... zero errors. Zero warnings. A small victory.

I execute the binary in the little black screen, type in the data, press Enter and... there it is. The coldest, cruelest, and most destructive message a computer can drop on you at this hour:

Segmentation fault (core dumped)

Silence in the room. I feel like throwing the keyboard out the window. Another sleepless night where the compiler tricked me into believing everything was fine. If you've been writing C code lately, you know exactly what pain I'm talking about.

What is a pointer really and why does it hurt so much?

The problem with C is that it doesn't hold your hand. If you come from tinkering with Visual Basic or higher-level stuff, this is a head-on collision. In C, you are the absolute master of the machine.

A pointer is nothing more than a variable that stores a memory address. Instead of saving a number like a 5, it saves the physical spot in the RAM where that 5 is. The problem comes when C tells you: "Here you have direct access to memory. Do whatever you want. If you make a mistake and write in a spot reserved by the operating system, I'll let you try and then I'll kill your process without explanation".

That power is incredible because it allows you to control performance down to the millimeter, but it's a constant danger. One little slip and you're trying to write into limbo, corrupting the memory architecture of your own program.

The cursed code

So you can see what I mean, this is the kind of stupidity that has me bitter today. Look at this piece of code:

#include <stdio.h>

int main() {
    int *mi_puntero;  // We declare the pointer, but... it doesn't point to anything specific!

    // We try to save a 42 in the address it points to
    *mi_puntero = 42; 

    printf("El valor es: %d\n", *mi_puntero);
    return 0;
}

You see it, right? The GCC compiler swallows it without a peep. Everything seems syntactically correct. But when the program runs, it tries to write the 42 into a random garbage memory address. The operating system realizes the intrusion and cuts to the chase, causing the infamous Segmentation fault.

My survival tricks

Through hard knocks, asking around in IRC programming channels, and reading English forums with a dictionary by my side, I've compiled some survival tactics:

  • Draw the memory on paper: It sounds stupid, but it works. Grab a sheet of paper and draw little boxes. Draw arrows pointing from one box to another. When you try to understand linked lists or double pointers just by looking at the screen, your brain collapses.
  • The brute-force printf technique: Since we have no magic tools here, my best friend for debugging is stuffing the code with printf("Got here 1\n");, printf("Got here 2\n");. That way you narrow down exactly which line the program blows up on. Is it ugly? Yes, but it works for me.
  • Always initialize to NULL: If you declare a pointer and don't know where it's going to point yet, set it to NULL (int *p = NULL;). It's much better for the program to break because you've used a null pointer (which is obvious to detect) than for it to break for writing to a random memory address that sometimes works and sometimes doesn't.
  • Watch out for arrays: Always remember that in C indexes start at zero. If your array has 10 positions and you try to access array[10], you are going outside the reserved memory. Runaway pointer guaranteed.

Is the suffering worth it?

Despite the headaches, the Core dumpeds, and the lost hours chasing ghosts in the RAM, learning C changes the way you think.

It forces you to understand how the computer works inside. You are no longer an advanced user using commands you don't understand; you begin to understand how the hardware manages data. It makes you more meticulous, more organized, and honestly, a much better programmer.

Now, if you'll excuse me, I'm going to put a few more printfs in my code to see if I can manage to hand in the assignment before dawn.