Segmentation fault

This is a discussion on Segmentation fault within the C++ Programming forums, part of the General Programming Boards category; I'm making a program to work with very large integers. The numbers are stored as a dynamic array of bytes. ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Segmentation fault

    I'm making a program to work with very large integers. The numbers are stored as a dynamic array of bytes. The arithmetic operations I've implemented involve a lot of brute force, and a lot of temporary arrays which are soon deleted.

    Everything works fine for low numbers, but for larger numbers like 25^400, I start getting segmentation faults at strange places. After many repetitions of the same code, a segmentation fault suddenly occurs when I delete a temporary array. The fault doesn't occur for smaller numbers, and it doesn't occur every time that particular array is deleted. If I comment out that delete line, a segmentation fault now occurs at another line, now on a 'new' allocation.

    The code is long and boring, but I could post some if anyone really wants. Does anyone have any general advice about segmentation faults and how to find their cause?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    but for larger numbers like 25^400, I start getting segmentation faults at strange places.
    I would check for array bounds errors.
    Kurt

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I've done that pretty thoroughly. I'll recheck again though. I'm also going to try compress the code down as much as I can, but so much code is interrelated, so it might be difficult.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Maybe this ?
    http://www.softwareverify.com/faq.html
    Your wizzy compiler should have some tools to help you track down such problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I'll have a look at that, thanks. In the meantime, here's some code which I have shrunk as much as possible. The error is still occuring.
    Attached Files Attached Files
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I increased how high the code above can calculate by truncating the numbers when they have too many leading zeros. However, I still get segmentation faults at around 3^92, and I need to go far higher than that.

    Does that code work for anyone? If it does, increase the exponent until it faults and then let me know how large you got it.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I added some counters to check if I'm destructing all instances and deleting all memory. I lowered the exponent just enough so that I wouldn't get a seg fault, and both counters were at zero, indicating that everything is being disposed of properly. Then from whence cometh this seg fault?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I found the problem. It was a sly, sneaky out of bounds error. All it did was corrupt the heap behind the scenes, while still allowing the correct answer to be output if the numbers were small enough. Here is where it was:

    Code:
                    int iSrc=(a.nSize*8)-1;
                    int iDest=(Terms[c]->nSize*8); //<!!!!!!!!!!! Should subtract there
    
                    while (iDest>(8*i)+j+(a.nSize*8)-1) {
                        if (static_cast<int>(iDest/8)>=ubound) std::cout << "<oob> " << std::flush;
                        SETBIT(Terms[c]->pNum[static_cast<int>(iDest/8)],iDest%8,0);
                        iDest--;
                    }
                    while (iSrc>=0) {
                        int BitSet=GETBIT(a.pNum[static_cast<int>(iSrc/8)],iSrc%8);
                        if (static_cast<int>(iDest/8)>=ubound) std::cout << "<oob> " << std::flush;
                        SETBIT(Terms[c]->pNum[static_cast<int>(iDest/8)],iDest%8,BitSet);
                        iSrc--;
                        iDest--;
                    }
                    while (iDest>=0) {
                        if (static_cast<int>(iDest/8)>=ubound) std::cout << "<oob> " << std::flush;
                        SETBIT(Terms[c]->pNum[static_cast<int>(iDest/8)],iDest%8,0);
                        iDest--;
                    }
    The effect of that tiny error was that a single byte beyond the allocated memory was being zeroed hundreds of times. I guess that caused some heap damage or hurt its feelings or something.

    I can now push it pretty high. I tried 3^5000, it took 5 seconds but it worked.

    Any tips on how to speed it up?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21