Weird Segfualt when calculating abundance

This is a discussion on Weird Segfualt when calculating abundance within the C++ Programming forums, part of the General Programming Boards category; I wrote this little app today to do the simple, maybe even trivial, task of seeing if a numbers abundant. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Weird Segfualt when calculating abundance

    I wrote this little app today to do the simple, maybe even trivial, task of seeing if a numbers abundant. Heres my code:
    Code:
    /******************************************
    ***                                     ***
    ***                                     ***
    *** Abundant Number - A number that is  ***
    *** less than the sum of its factors    ***
    *** (excluding itself). For example,    ***
    *** the factors of 12 (1, 2, 3, 4, 6)   ***
    *** add up to 16. Compare deficient     ***
    *** number, perfect number.             ***
    *******************************************/
    
    #include <iostream.h>
    #include <stdlib.h>
    
    void abundant(int org);      //Our function to check the abudance of a number
    int main(int argc, char *argv[])
    {
    
       /*
        * If theres no arguments present then print a error message
        * and exit.
        */
       if( argc < 2 ){
          cout << "Usage: " << argv[0] << " <test number>\n";
          cout << "<test number> is the number to test for abundance.\n";
          exit(0);
          }
    
       /*
        * Convert the first argument to a integer.
        */
       int test;
       test = atoi(argv[1]);
    
       /*
        * If the integers value is higher then 200 then print a error
        * message and quit.
        */
       if( test > 200 ){
          cout << "This application only accepts numbers between 0 - 200\n";
          exit(0);
          }
    
       /*
        * Now we're sure that the number is applicable to our checks we
        * call our abundance checking function. The only argument to
        * this function is the integer named test. (The users argument)
        */
       abundant(test);
       return 0;
    }
    
    void abundant(int org)
    {
       int i, j, test;
       int intarr[200];
       j = 0;
    
       /*
        * Declare a loop. The loop will execute "org" amount of time. Org
        * is the number we are testing.
        */
       for(i=0; i >= org; i++)
       {
          /*
           * When we divide "org" by "i" if theres no remainder then we will
           * add it to our integer array. This just stores "org"s factors.
           */
          if(((org % i) == 0) && (i != org))
          {
             intarr[j] = i;
             j++;
          }
       }
    
       /*
        *  Now we loop through our array of factors and add them all up.
        */
       for(i=0; i >= j; i++)
       {
          test += intarr[i];
       }
    
       /*
        * If the sum of factors is greater than the number we are testing
        * then the number is abundant.
        */
       if( test > org){
       cout << org << " - The number is abundant.\n";
       return;
       }
    
       /*
        * Else, if the sum of factors is equal to, or less then the
        * number we are testing we display a message with our results.
        * In this case our results are that the number ISN'T abundant.
        */
       else if( test <= org){
       cout << org << "Isn't a abundant number.\n";
       return;
       }
    }
    It compiled ok and I proceeded to run it. I supplied it with numbers in the correct range and it simply "crapped out". When I use a number above 200 it works perfect. (i.e displays the error message)

    If I don't supply it with any arguments it does what its meant to and displays the usage.

    When I supply it with the correct info it simply does nothing.

    I tried to debug it with gdb, as I have only used gdb on my linux box I wasnt sure if the "no debuging symbols" was ok.

    (no debugging symbols found)...(gdb)

    (gdb) run 100
    run 100
    Starting program: \DOCUME~1\Fergus\MYDOCU~1/Abudance.exe 100
    77f50000:ntdll.dllntdll.dll: No such file or directory.
    77e60000:C:/WINDOWS/system32/kernel32.dll(no debugging symbols found)...
    77c10000:C:/WINDOWS/system32/msvcrt.dll(no debugging symbols found)...

    Program received signal SIGSEGV, Segmentation fault.
    0x401432 in ?? ()
    (gdb)
    It throws a segfualt out. I simply don't understand how my program is accessing memory out of its allowed range.

    Any ideas or Suggestions?
    Thanks!
    -Moddy

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Your problem is in this section:

    Code:
       /*
        *  Now we loop through our array of factors and add them all up.
        */
       for(i=0; i >= j; i++)
       {
          test += intarr[i];
       }
    Sent from my iPadŽ

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Actually, allow me to explain better.

    You're not entering this loop at all:
    Code:
       for(i=0; i >= org; i++)
       {
          /*
           * When we divide "org" by "i" if theres no remainder then we will
           * add it to our integer array. This just stores "org"s factors.
           */
          if(((org % i) == 0) && (i != org))
          {
             intarr[j] = i;
             j++;
             cout << "Test";          
          }
       }
    Which is causing this loop to be infinite.
    Code:
       /*
        *  Now we loop through our array of factors and add them all up.
        */
       for(i=0; i >= j; i++)
       {
          test += intarr[i];
       }
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Quote Originally Posted by SlyMaelstrom
    Your problem is in this section:

    Code:
       /*
        *  Now we loop through our array of factors and add them all up.
        */
       for(i=0; i >= j; i++)
       {
          test += intarr[i];
       }
    Ah, I see now! I hav re-read my code and the for statements seem wrong. (i.e >= instead of <=) That if statement also looks incorrect (AND instead of OR).

    I can't see any reasons not to enter the loop though. =/

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Moddy
    Ah, I see now! I hav re-read my code and the for statements seem wrong. (i.e >= instead of <=) That if statement also looks incorrect (AND instead of OR).

    I can't see any reasons not to enter the loop though. =/
    What does i equal when you initially try to enter the loop? For-loops only execute if the for-loop conditional equals true, and the conditional is tested at the start of every loop. Is the for-loop conditional true or false?
    Last edited by 7stud; 11-06-2005 at 02:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 11:23 PM
  2. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2006, 12:08 AM
  3. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 08:32 AM
  4. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 01:00 AM
  5. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 05:47 PM

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