7

Why am I getting segmentation fault 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;

};

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

falconmick
  • 79
  • 1
  • 1
  • 2
  • Also... It runs on previous itterations without fault... It just doesnt like this iteration... I would link the code but theres buckets of the stuff X( – falconmick May 02 '11 at 11:34
  • 1
    I don't think your problem is that function. You may want to take a look at how you are creating/accessing the person object you are using. – Cole W May 02 '11 at 11:35
  • Your code example is alright, the segfault is caused somewhere else. Please step through your code with a debugger to find the line of code causing the error. – Ferdinand Beyer May 02 '11 at 11:36
  • 1
    @falconmick: Then please reduce your code to the *minimum* possible and post it here. There's no issue with the above code, in isolation, at least. – Oliver Charlesworth May 02 '11 at 11:37
  • also the varible personName equaled (in my watches) "the program being debugged was signaled while in a function call from gdb" – falconmick May 02 '11 at 11:39
  • 1
    Um, are you saying that that string "the program being debugged..." was the contents of that variable? Very strange. Can you tell us which line of code the fault is on? Build the program with -g (if using GCC), then run it in GDB. It will break on the error. If you don't recognise where you are, then type `bt` which will show the function call stack. Look up the stack until you find some of your code -- that is the line with the error. – mgiuca May 02 '11 at 11:44
  • 1
    @mgiuca: To be precise, that would be the line with the *symptom*. The actual error could be elsewhere. – Oliver Charlesworth May 02 '11 at 11:48
  • @mgiuca: gdb is just confirming that the this pointer is invalid at the time of evaluation. +1 for using a debugger, btw – sehe May 02 '11 at 11:52
  • @Oli Chalesworth / @sehe indeed. I was trying to simplify things but perhaps I over-simplified. To show that I know what I'm on about ;) I wrote a guide to debugging these issues: http://ww2.cs.mu.oz.au/~mgiuca/253/bad.html Perhaps it will be helpful. – mgiuca May 02 '11 at 12:04
  • @mguica: Ha - nice find (bgcc, formerly not known to me). However, like debugging STL iterators and debugging heap allocators, they all pale in comparison to the sheer fundamentalism of valgrind; it does positively check for **any unitialized/undefined** memory references, be it within or out of bounds (in addition to doing the leak checking) - all without even requiring debug builds or any specific compiler. That rocks – sehe May 02 '11 at 17:02
  • Read [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Then load your core dump into the debugger and see what's going on. – Jesper Juhl Apr 10 '23 at 22:05

3 Answers3

21

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".

mgiuca
  • 20,958
  • 7
  • 54
  • 70
1

Probably, you are dereferencing a rogue pointer. By pure guesswork, have you got something like this, perhaps:

 Person persons[10];

 for (i=1; i<=10; i++)
     persons[i].setName("joel");

The problem might be:

  • the error like shown, the index is 0-based, so you need for (i=0; i<10; i++)
  • if the array is allocated dynamically, but the index is still out of bounds

There could literally be hundreds of other causes, but since I don't have your code, this is my attempt to guess most plausible errors ;)

(Note to self: why am I doing this/I'm not psychic?)

sehe
  • 374,641
  • 47
  • 450
  • 633
-1
#include <iostream>

using namespace std;

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

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

};

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

int main() {
  person* obj = new person();
  obj->setName("your name");
  return 0;
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 13 '23 at 11:40
  • Please add at least some comments to your code to explain what it does and how it's supposed to answer the question. – Abderrahmene Rayene Mihoub Apr 15 '23 at 20:39