0

I'm receiving this error from my compiler "segmentation fault: core dump". I assume it's from the function I had written but not exactly sure what about the function is wrong.

The program reads from the input file and it outputs results on the screen (cout).

string rna_complement(string line)
{

for(int i = 0; i < line.length(); i++)
{
        switch (line[i])
        {
            case 'A': line[i] = 'U'; break;
            case 'C': line[i] = 'G'; break;
            case 'G': line[i] = 'C'; break;
            case 'U': line[i] = 'A'; break;
            }
}


cout <<"Complement: " <<line <<endl;
}



int main()
{
string line1, line2, line3, line4, line5, line6;
ifstream genesacid;

genesacid.open("genes.txt");
    if(!genesacid.is_open())
    {
        cerr <<"Error: File cannot be opened"<<endl;
        return 0;
    }

            else
            {
                getline(genesacid, line1);
                getline(genesacid, line2);
                getline(genesacid, line3);
                getline(genesacid, line4);
                getline(genesacid, line5);
                getline(genesacid, line6);
            }



cout << "Fragment 1: " <<line1 <<endl;
rna_complement(line1);


cout << "Fragment 2: " <<line2 <<endl;

genesacid.close();


return 0;
}

I'm not very adept in C++ so if any errors you see is obvious, please be mindful of this. I'm slowly learning!

8
  • Can you debug step by step to locate where error happens? Commented Mar 19, 2013 at 22:15
  • 3
    Where does the segmentation fault happen? (On which line?) Commented Mar 19, 2013 at 22:15
  • You might start by fixing the inconsistent indentation so we can understand the code better Commented Mar 19, 2013 at 22:16
  • 1
    The report of the segmentation fault comes not from the compiler, but from the runtime/OS. You can and should run it using a debugger and this will often automatically indicate the spot in the source that triggered the fault. Commented Mar 19, 2013 at 22:17
  • it doesnt say which line it happens but when i run the program, it outputs the "fragment 1" then runs the function (which outputs a line) then it displays the error. Commented Mar 19, 2013 at 22:18

2 Answers 2

3

I'm guessing it is because you forgot to return something in your function. A seg fault can happen because of this. Also, make sure you have your warning level on high, a good compiler will usually let you know about this.

Here is the C++ standard 6.6.3:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

Sign up to request clarification or add additional context in comments.

4 Comments

For clarity, forgetting to do so is undefined behaviour, and equivalent to return;.
@chris: Thanks, I added the quote from the standard.
Frankly, the standard is deficient in this case. Ideally, it should be modified to clarify that flowing off the end of such a function should result in a malformed C++ program and a hard error. But that won't happen, so I'd settle for mandating at least a warning.
@NikBougalis, Yes, I do forget to do this from time to time. Thankfully, all major compilers I know of do have something to say about this. MSVC gives an error (which is non-standard as far as I'm aware, but the program is simply broken anyway) and GCC and Clang at least warn with the right warning level. I'm very grateful that it's this easy to catch errors.
0

If you don't return anything in the function rna_complement, it should be void. The segmentation fault comes from the call of the destructor of std::string. Alternatively, you can return line since it is a copy. If you want in-place modification of the string, you can pass a reference to it (string &line), and it will modify the string passed in argument.

2 Comments

Odd, I've never used a compiler that didn't give a 'Warning: control reached end of non-void function' or similar in this situation.
clang would complain by default without any flag. gcc without any flag won't complain. gcc -Wall would. Oddly gcc -pedantic doesn't seem to care either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.