简体   繁体   English

程序接收信号 SIGSEGV,Segmentation fault

[英]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好的......我正在扯掉我的头发......当我将一个名为“name”的字符串传递给内容“joel”时,为什么会出现分段错误

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

Header file: Header 文件:

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.顺便说一句... function 电话是由一个孩子来的,尽管我不明白这会如何导致问题。

If you are on Linux, try running valgrind .如果您使用的是 Linux,请尝试运行valgrind You just compile with -g (with gcc), and then run your program with valgrind in front:您只需使用-g (使用 gcc)进行编译,然后在前面使用valgrind运行您的程序:

$ 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.与 GCC 解决方案不同,后者会告诉您何时发生段错误,而 valgrind 通常会准确地告诉您第一次 memory 损坏发生的时间,因此您可以更接近问题的根源。

PS. PS。 It rhymes with "flint", not "find".它与“flint”押韵,而不是“find”。

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++)如图所示的错误,索引是从0开始的,所以你需要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? ) 自我注意:我为什么要这样做/我不是通灵者?

The code looks fine apart from the fact that you copying string all the time.除了您一直在复制字符串这一事实之外,该代码看起来还不错。 Instead of代替

void setName(string newName);

should be应该

void setName(const string& newName);

The issue must be in the method invocation.问题必须在方法调用中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-程序收到信号SIGSEGV,分段错误 - Python - Program received signal SIGSEGV, Segmentation fault 程序收到信号SIGSEGV,分段错误 - program received signal SIGSEGV, segmentation fault 程序接收到信号SIGSEGV,输出出现分段故障 - Program received signal SIGSEGV, Segmentation fault in output 编程接收到的信号SIGSEGV,在代码块中调试时出现分段错误 - Program received signal SIGSEGV, Segmentation fault while debugging in codeblocks 程序接收到的信号SIGSEGV,吸气方法中的分段错误 - Program received signal SIGSEGV, Segmentation fault in getter method strcpy 原因程序收到信号SIGSEGV,分段错误 - strcpy cause Program received signal SIGSEGV, Segmentation fault 程序收到信号 SIGSEGV,分段错误。 C++ - Program received signal SIGSEGV, Segmentation fault. C++ “程序接收信号SIGSEGV,分段故障。在??? ()()“在Code :: Blocks中调试我的C ++项目时 - “Program received signal SIGSEGV, Segmentation fault. In ?? () ()” when debugging my C++ project in Code::Blocks C++ - 程序收到信号 SIGSEGV,分段错误。在 msvcrt!memcpy () (C:\\Windows\\System32\\msvcrt.dll) - C++ - Program received signal SIGSEGV, Segmentation fault.In msvcrt!memcpy () (C:\Windows\System32\msvcrt.dll) 程序收到信号SIGSEGV,分段故障。 在al_draw_tinted_bitmap中(位图= 0x0,色调= ...,dx = 0,dy = 0,标志= 0) - Program received signal SIGSEGV, Segmentation fault. in al_draw_tinted_bitmap (bitmap=0x0, tint=…, dx=0, dy=0, flags=0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM