-4

I'm new to using c++ so this may be a vector issue. I'm getting a floating point error. I don't think this is a divide by zero error. Any help would be appreciated.

The code compiles, but when run the error pops up. "Floating point exception (core dumped)"

    vector<int> radixSort (vector<int> &numbs, int maxA)
    {
        vector<vector<int> >temp1;
        vector<vector<int> >temp2;

        for (int i = 0; i < 10 ; i++)
            {
                temp1.push_back(vector<int>());
                temp2.push_back(vector<int>());
            }

        int x = 1;
        int plc = 0;
        while (maxA/x > 0)
        {
            plc = plc + 1;
            x = x * 10;
        }
        for (int i = 0; i < (int)numbs.size(); i++)
        {
            int aryLoc = numbs[i] % 10;
            temp1[aryLoc].push_back(numbs[i]);
        }
        plc = plc - 1;
        int modDiv = 100;
        int intDiv = 10;
        bool isTemp1 = true;
        while (plc > 0)
        {
            for (int i = 0; i < 10 ; i++)
            {
                if (temp1[i].size() > 0)
                {
                    for (int j = 0; j < temp1[i].size() ; j++)
                    {
                        int aryLoc = temp1[i][j] % modDiv;
                        aryLoc = temp1[i][j] / intDiv;
                        temp2[aryLoc].push_back(numbs[i]);
                        modDiv = modDiv * 10;
                        intDiv = intDiv * 10;
                    }
                }
            }
            plc = plc - 1;
            isTemp1 = false;
            for (int i = 0; i < 10 ; i++)
            {
                temp1[i].clear();
            }
            if (plc > 0)
            {
                for (int i = 0; i < 10 ; i++)
                {
                    if (temp2[i].size() > 0)
                    {
                        for (int j = 0; j < temp2[i].size() ; j++)
                        {
                            int aryLoc = temp2[i][j] % modDiv;
                            aryLoc = temp2[i][j] / intDiv;
                            temp1[aryLoc].push_back(numbs[i]);
                            modDiv = modDiv * 10;
                            intDiv = intDiv * 10;
                        }
                    }
                }
                plc = plc - 1;
                isTemp1 = true;
                for (int i = 0; i < 10 ; i++)
                {
                    temp2[i].clear();
                }
            }
        }

        if (isTemp1 == true)
        {
            int y = 0;
            for (int i = 0; i < 10 ; i++)
            {
                if (temp1[i].size() > 0)
                {
                    for (int j = 0; j < temp1[i].size() ; j++)
                    {
                        numbs[y] = temp1[i][j];
                        y++;
                    }
                }
            }
        }

        else
        {
            int y = 0;
            for (int i = 0; i < 10 ; i++)
            {
                if (temp2[i].size() > 0)
                {
                    for (int j = 0; j < temp2[i].size() ; j++)
                    {
                        numbs[y] = temp2[i][j];
                        y++;
                    }
                }
            }
        }

        return numbs;
    }

thanks for the help seems to be working now when I found my errors here is the fixed copy

vector<int> radixSort (vector<int> &numbs, int maxA)
{
    vector<vector<int> >temp1;
    vector<vector<int> >temp2;

    for (int i = 0; i < 10 ; i++)
        {
            temp1.push_back(vector<int>());
            temp2.push_back(vector<int>());
        }

    int x = 1;
    int plc = 0;
    while (maxA/x > 0)
    {
        plc = plc + 1;
        x = x * 10;
    }
    for (int i = 0; i < (int)numbs.size(); i++)
    {
        int aryLoc = numbs[i] % 10;
        temp1[aryLoc].push_back(numbs[i]);
    }
    plc = plc - 1;
    int modDiv = 100;
    int intDiv = 10;
    bool isTemp1 = true;
    while (plc > 0)
    {
        for (int i = 0; i < 10 ; i++)
        {
            if (temp1[i].size() > 0)
            {
                for (int j = 0; j < temp1[i].size() ; j++)
                {
                    int aryLoc = temp1[i][j] % modDiv;
                    aryLoc = aryLoc / intDiv;
                    temp2[aryLoc].push_back(temp1[i][j]);
                }
            }
        }
        modDiv = modDiv * 10;
        intDiv = intDiv * 10;
        plc = plc - 1;
        isTemp1 = false;
        for (int i = 0; i < 10 ; i++)
        {
            temp1[i].clear();
        }
        if (plc > 0)
        {
            for (int i = 0; i < 10 ; i++)
            {
                if (temp2[i].size() > 0)
                {
                    for (int j = 0; j < temp2[i].size() ; j++)
                    {
                        int aryLoc = temp2[i][j] % modDiv;
                        aryLoc = aryLoc / intDiv;
                        temp1[aryLoc].push_back(temp2[i][j]);
                    }
                }
            }
            modDiv = modDiv * 10;
            intDiv = intDiv * 10;
            plc = plc - 1;
            isTemp1 = true;
            for (int i = 0; i < 10 ; i++)
            {
                temp2[i].clear();
            }
        }
    }

    if (isTemp1 == true)
    {
        int y = 0;
        for (int i = 0; i < 10 ; i++)
        {
            if (temp1[i].size() > 0)
            {
                for (int j = 0; j < temp1[i].size() ; j++)
                {
                    numbs[y] = temp1[i][j];
                    y++;
                }
            }
        }
    }

    else
    {
        int y = 0;
        for (int i = 0; i < 10 ; i++)
        {
            if (temp2[i].size() > 0)
            {
                for (int j = 0; j < temp2[i].size() ; j++)
                {
                    numbs[y] = temp2[i][j];
                    y++;
                }
            }
        }
    }

    return numbs;
}
brimboch
  • 3
  • 2
  • 5
    [Debugger](http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html) tutorial – Suvarna Pattayil Jun 14 '13 at 10:42
  • 1
    Have you tried debugging? Which line throws exception? or, which part of code can be suspected? – raj raj Jun 14 '13 at 10:42
  • The code is enough length. What IDE do you use? Did you tried to run under debugger to see what line exacly leads to error? – Ivan Aksamentov - Drop Jun 14 '13 at 10:42
  • It says "core dumped". So it created a file, called `core` (possibly with some suffix) that you can open with debugger and the debugger will than let you inspect content of variables and stack. You need to get at least back trace. If it does not hint you anything, add it to the question and somebody might be able to help you understand it. But without backtrace it's unlikely anybody could help you. – Jan Hudec Jun 14 '13 at 10:46
  • You should also provide the numbs and maxA parameters. I can't reproduce it with the values I'm trying. – ctn Jun 14 '13 at 10:50
  • 1
    By the way, the code does not use any floating point numbers. At all. Since vector does not use any internally either, the problem is likely somewhere else altogether (@ctn I don't think you can reproduce floating point exception with piece of code that does not say either `float` or `double` anywhere). – Jan Hudec Jun 14 '13 at 10:50
  • 2
    @JanHudec On at least some architectures, an integer division by zero, or `INT_MIN / -1` will cause a floating point exception. Bad naming for hysterical raisins. – Daniel Fischer Jun 14 '13 at 10:52
  • @DanielFischer: Hm, true. It is Undefined Behaviour™ and that can do anything. Including raising floating point exception. – Jan Hudec Jun 14 '13 at 10:57
  • @SuvP Reading that, I stumbled over "1 << 31 (the integer 1 right-shifted 31 times) is 429497295, or 4GB (gigabytes)" and "Change the 1<<31 to 1024 (or 1<<9), and the program will work as expected". That doesn't mean the debugger-tutorial must be bad, but I'm a bit wary of tutorials getting such basic stuff fundamentally wrong. – Daniel Fischer Jun 14 '13 at 10:58
  • 1
    @JanHudec: Something about SIGFPE [here](http://stackoverflow.com/questions/1081250/why-is-this-a-floating-point-exception). – raj raj Jun 14 '13 at 11:06
  • @DanielFischer I agree (my reaction was the same when I landed on the page for the 1st time). I like the tutorial because it gives a quick overview of how gdb can be used for debugging. (I hope no one learns the *erroneous* debugging logic from it :P ) – Suvarna Pattayil Jun 14 '13 at 11:13
  • I'm writing the code in notepad++ and compiling in g++ on a unix server. What should I use to debug and step through the code? – brimboch Jun 14 '13 at 12:19
  • @user2485670 YOu can use [this](http://www.gnu.org/software/gdb/) – Suvarna Pattayil Jun 14 '13 at 12:40

1 Answers1

1

First, your multiplications of modDiv and intDiv are happening in the wrong place, but I'll leave you to figure it out.
Also, your computation of aryLoc is a bit off - it's not always between 0 and 9.
And there's what's most likely a copy-paste bug when you're moving elements between vectors.

But regarding this particular error: amusingly, "floating point exception" doesn't necessarily imply a floating point error, it can happen for instance if you divide an integer by zero.

If the vector is sufficently large (and depending on maxA and the values in the vector), modDiv will overflow, and that is undefined behaviour.
In other words, anything can happen.

I'm not sure what maxA signifies - I have set it to the greatest value in the vector when testing.

With my g++, I get this output if I log modDiv through the loops:

> ./sort 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 100000
modDiv: 1000
modDiv: 10000
modDiv: 100000
modDiv: 1000000
modDiv: 10000000
modDiv: 100000000
modDiv: 1000000000
modDiv: 1410065408
modDiv: 1215752192
modDiv: -727379968
modDiv: 1316134912
modDiv: 276447232
modDiv: -1530494976
modDiv: 1874919424
modDiv: 1569325056
modDiv: -1486618624
modDiv: -1981284352
modDiv: 1661992960
modDiv: -559939584
modDiv: -1304428544
modDiv: -159383552
modDiv: -1593835520
modDiv: 1241513984
modDiv: -469762048
modDiv: -402653184
modDiv: 268435456
modDiv: -1610612736
modDiv: 1073741824
modDiv: -2147483648
modDiv: 0
Floating point exception (core dumped)

So at least in my case, "anything can happen" ended, after a slight detour around the int range, with a modulus by zero.

This bug should go away when you get your multiplications right.
Fixing aryLoc should take care of the other core dumps you will run into.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82