Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes a SIGSEGV

I need to know the root cause of the segmentation fault (SIGSEGV), and how to handle it.

like image 699
Vaibhav Avatar asked Oct 14 '09 05:10

Vaibhav


People also ask

What is return code SIGSEGV?

A. A SIGSEGV is an error(signal) caused by an invalid memory reference or a segmentation fault. You are probably trying to access an array element out of bounds or trying to use too much memory.

How do I fix SIGSEGV error?

Make sure you aren't using variables that haven't been initialised. These may be set to 0 on your computer, but aren't guaranteed to be on the judge. Check every single occurrence of accessing an array element and see if it could possibly be out of bounds. Make sure you aren't declaring too much memory.

How do you avoid SIGSEGV?

Avoid naked pointers (prefer smart pointers, such as std::unique_ptr or std::shared_ptr for pointers that own data, and use iterators into standard containers if you want to merely point at stuff) Use standard containers (e.g. std::vector ) instead of arrays and pointer arithmetics.

What does signal SIGSEGV mean?

The SIGSEGV signal is raised when you attempt to illegally access or modify memory. SIGSEGV is usually caused by using uninitialized or NULL pointer values or by memory overlays.


1 Answers

Wikipedia has the answer, along with a number of other sources.

A segfault basically means you did something bad with pointers. This is probably a segfault:

char *c = NULL; ... *c; // dereferencing a NULL pointer 

Or this:

char *c = "Hello"; ... c[10] = 'z'; // out of bounds, or in this case, writing into read-only memory 

Or maybe this:

char *c = new char[10]; ... delete [] c; ... c[2] = 'z'; // accessing freed memory 

Same basic principle in each case - you're doing something with memory that isn't yours.

like image 151
Chris Lutz Avatar answered Sep 20 '22 16:09

Chris Lutz