Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program received signal SIGSEGV, Segmentation fault

Ok... I am ripping my hair out... Why am I getting segmentation fauls when I am passing a string called "name" with contents "joel" into

void person::setName(string newName)
{
    personName = newName;
}

Header file:

class person {
public:
    int getID();
    string getName();

    void setID(int newID);
    void setName(string newName);
private:
    int personID;
    string personName;

};

btw... the function call is by a child, although I dont see how that could cause an issue.

like image 655
falconmick Avatar asked May 02 '11 11:05

falconmick


People also ask

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.

What causes SIGSEGV?

A SIGSEGV signal or segmentation error occurs when a process attempts to use a memory address that was not assigned to it by the MMU.

How do you avoid SIGSEGV errors?

Don't use pointers. And if you do always check for NULL pointers. Minimize the use of pointers, access any kind of arrays within its bounds, check whether pointer is NULL before using, make sure you allocate new memory before using a pointer which you have already freed (it may not be giving NULL ).

What does SIGSEGV stand for?

On Unix-like operating systems, a signal called SIGSEGV (abbreviated from segmentation violation) is sent to the offending process.


1 Answers

If you are on Linux, try running valgrind. You just compile with -g (with gcc), and then run your program with valgrind in front:

$ valgrind myprogram

Unlike the GCC solutions, which tell you when the segfault occurs, valgrind usually tells you exactly when the first memory corruption occurs, so you can catch the problem much closer to its source.

PS. It rhymes with "flint", not "find".

like image 192
mgiuca Avatar answered Sep 21 '22 12:09

mgiuca