> Is there some kind of "magic" that gcc does when it compiles the code? Maybe it allocates more space, even if I write malloc(1)?
You could try either

a) running valgrind, as in
valgrind ./a.out

b) linking with electric fence, and running in the debugger
gcc prog.c -lefence
gdb ./a.out

Both will tell you if you're abusing the space allocated through malloc (and a bunch of other stuff as well in the case of valgrind).

FWIW, your malloc is 1 byte short.
You forgot to count the \0.