A sight like this image above is pretty common. But have you ever wondered why there are stones in between and underneath those rail tracks?
Trains
can be extremely heavy, this weight is focused upon the fairly small
area of their wheels. The stones are actually called track ballast
and help to spread the huge force from the train's wheels out over a
larger area of ground. Without this ballast the ground underneath might
sink unevenly. The sleepers (cross ties) of the tracks are not
directly attached to the ballast which allows the track to have a little
movement (e.g. as the track expands and contracts due to changes in
temperature).
Stones are a good choice for this role because they are relatively cheap when compared to alternate materials, they can resist compressive loads well and require very low maintenance.
Also of note: Ballast is itself built upon a foundation of earth (the subgrade) that helps to raise the track and further distribute load.
Other functions: Stops plants growing around the tracks, allows water to drain away. Thus it is essential in maintaining the stability of the ground.
Are rocks a significant derailment hazard? Not really -- trains are
massive and move quickly -- this enormous momentum means they smash
right through most anything in their path. Rocks are typically turned to
dust by the wheels of the train or thrown out of the way by the
tremendous pressure of the wheels against the track.