It is safe to cook crabs that have been dead only a few hours. If unsure how long the crab has been deceased, avoid cooking it.
Cooking crabs generally requires live or recently dead crab. For some cooks, handling live crab is a problem and the crab is euthanized via either a blow to the abdomen or the insertion of a sharp object like an ice pick to the crab's "head." Both these methods kill the crab prior to cooking.
Once a crab has been dead for more than a few hours, the meat inside the shell begins to decompose because of the way the digestive system of the crustacean works.