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.
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.
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.
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 ).
On Unix-like operating systems, a signal called SIGSEGV (abbreviated from segmentation violation) is sent to the offending process.
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".
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