Malloc is used to allocate memory on the fly for dynamic data structures (e.g. Linked lists). But it is very easy to make mistakes.
The example program 'badmalloc' (gcc badmalloc.c -g -o badmalloc ) allocates memory but then writes over it and some memory after the allocated area. Running this under gdb might show a segfault in malloc itself (at a later call), or it might just happen to work if nothing important was overwritten.