I need to know the root cause of the segmentation fault (SIGSEGV), and how to handle it.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With