Main Content

Segmentation Fault, Segmentation Violation, Bus Error, Stack Smashing

Archive - Originally posted on "The Horse's Mouth" - 2014-12-04 06:07:27 - Graham Ellis

Have you ever had these error messages come up from your C program and wondered what they really mean?

Segmentation Fault

Segmentation Violation

Bus Error

stack smashing detected


A segmentation fault is when you try to access memory that doesn't exits. A segmentation violation is when you try to access memory that's not your process's. A bus error is when you try to an address that points to the memory bus. Stack Smashing is when you try to reduce the stack to less than zero in size.

But they really all mean the same thing - you've made a mistake with your addresses and pointers, called a function (in K & R C) with the wrong number of parameters (or got a wrong function prototype in ANSI C), or you've overrun an array and corrupted something.

A great deal of moving from being a new C and C++ programmer to an efficient and experienced one is about knowing how to naturally avoid messages like these in the first place, and about learning how to quickly spot the cause when they do occur.

Can I make a strong recommendation? Write your code in named blocks - functions or methods, build test programs for those named blocks, keep your tests so you can run and rerun them. You won't eliminate the segmentation, stack and bus errors - but you will help yourself find them in a relatively small area of code. Informally, I'm recommending that you move towards test driven, or behaviour driven, development!