Segmentation fault on string return

I'm trying to convert a decimal number to a binary number using a recursive function that returns a string. The correct string is stored in to numBin, but a segmentation fault is thrown when it is returned to the calling function (or it just crashes the terminal...hard). I cannot for the life of me figure out what is going on here. Please help!

the std::cout in the recursive function was added for debugging.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
std::string decToBin(int numDec, int binLength, std::string numBin)
{
    if (binLength == 0)
    {
        std::cout << numBin << std::endl;
        //Segmentation fault occurs here.
        return numBin;
    }    
    else if (numDec % 2 == 0)
    {
      numBin = '0' + numBin;
      decToBin(numDec/2,binLength-1,numBin);
    }
    else if (numDec % 2 == 1)
    {
      numBin = '1' + numBin2;
      decToBin(numDec/2,binLength-1,numBin);
    }
}

std::string decToBin(int numDec)
{
    //Count how many times the number can be divided by 2
    // using integer division to establish the length of the
    // binary number. binLength is used in the recursive function
    // as an indexer to end recursion and return the completed array.
    int binLength = 0, num = numDec;
    do
    {
        num/=2;
        binLength++;
    }while (num > 0);
    
    //Construct a string for use in the recursive function.
    std::string numBin;

    return decToBin(numDec, binLength, numBin);
}
Last edited on
You forgot the returns at the end of the two else ifs.

Also, note that decToBin() can be easily turned into an iterative function.
I don't want to return anything yet inside the else if's though. It would only return single binary bits, not the entire binary number. Right?

And yes, it would be much more simple as an iterative function, but I want to try to figure it out using recursive functions...call me stubborn.

Thank you for taking time to help me out with this!
Last edited on
I don't want to return anything yet inside the else if's though. It would only return single binary bits, not the entire binary number. Right?
You're not properly evaluating the logic of the code. The (presumably) correct implementation of your function is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::string decToBin(int numDec, int binLength, std::string numBin)
{
    if (binLength == 0)
    {
        std::cout << numBin << std::endl;
        return numBin;
    }    

    //Note: return transfers control back to the caller, so there's no
    //need to have an else here.
    if (numDec % 2 == 0)
    {
      numBin = '0' + numBin;
      return decToBin(numDec/2,binLength-1,numBin);
    }
    //Note: If !(numDec % 2 == 0) then the only possibility is numDec % 2 == 1
    else
    {
      numBin = '1' + numBin2;
      return decToBin(numDec/2,binLength-1,numBin);
    }
}
Try to figure out why.
Besides that, if a function has a return type different than void, it must have a return statement in every possible code path, or the program is invalid (though the compiler may accept it).
Last edited on
1
2
3
4
5
std::string dec_to_bin( unsigned int num_dec )
{
    if( num_dec < 2 ) return std::string( 1, num_dec + '0' ) ;
    return dec_to_bin( num_dec/2 ) + char( num_dec%2 + '0' ) ;
}

http://coliru.stacked-crooked.com/a/cf7bacc73dca9525
You're not properly evaluating the logic of the code. The (presumably) correct implementation of your function is.


You're totally right. I just wasn't quite wrapping my head around recursion correctly. Thank you for your help! That did it.
Topic archived. No new replies allowed.