Like Tree2Likes
  • 1 Post By memcpy
  • 1 Post By grumpy

C programming access violation segmentation fault question

This is a discussion on C programming access violation segmentation fault question within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm new to this sight and have a smallish amount of programming knowledge, so a simpler answer would ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    11

    C programming access violation segmentation fault question

    Hi everyone,
    I'm new to this sight and have a smallish amount of programming knowledge, so a simpler answer would be great.

    For some reason, whenever I try to run this code with a H 13 or higher, it says that the program stopped working. I am using the Dev compiler and have tried debugging, and it says it thinks the problem is in line "divisorList[arrayCounter]=divisorTest;". However, I can't seem to figure out what the problem is...
    Anything would be helpful.
    Code:
    #include <stdio.h>
    
    
    main()
    {
           int H, numOfHiddenDots, arraySlots, counterH, arrayCounter, arrayResetCounter, divisorTest, divisorTestTest, divisorRunthrough;
           numOfHiddenDots=0;
           arraySlots=1;
           int divisorList[arraySlots];
           
           H=(whatever you want)
           arraySlots=H+1;
           
           for (counterH=1; counterH<=H; counterH++)
           {
               arrayCounter=1;
               for (arrayResetCounter=0; arrayResetCounter<=counterH+1; arrayResetCounter++)
               {
                   divisorList[arrayResetCounter]=0;
               }
               if (counterH%100==0) printf ("%d milestone reached in calculations.\n", counterH);
               
               for (divisorTest=1; divisorTest<counterH; divisorTest++)
               {
                   if (counterH%divisorTest==0)
                   {
                      divisorList[arrayCounter]=divisorTest;
                      arrayCounter++;
                      
                      for (divisorTestTest=2; divisorTestTest<arrayCounter-1; divisorTestTest++)
                      {
                          if (divisorList[divisorTestTest]!=0&&divisorTest%divisorList[divisorTestTest]==0) divisorList[divisorTestTest]=0;
                      }
                   }
               }
               
               for (divisorRunthrough=1; divisorRunthrough<arrayCounter; divisorRunthrough++)
               {
                   if (divisorList[divisorRunthrough]<=1) numOfHiddenDots=numOfHiddenDots+divisorList[divisorRunthrough]*6;
                   else numOfHiddenDots=numOfHiddenDots+(divisorList[divisorRunthrough]-1)*6;
               }
           }
           
           printf ("The number of dots hidden from the center is: %d\n", numOfHiddenDots);
           getchar();
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    The array divisorList is always one place long. You can't declare a fixed array and then hope the boundaries will change when the variable denoting the size does.

    Your problem is most likely an array indexing issue, by trying to read/write over the bounds of stack memory that the array is in.
    quzah likes this.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,527
    You have defined divisorList as an array of one element.

    Doing
    Code:
        int arraySlots = 1;
        int divisorList[arraySlots];
        arraySlots = some_value_greater_than_1;
    does not magically resize the array divisorList. Any access of divisorList[i] where i is 1 or more will always fall off the end of the array.

    You just got lucky (or unlucky, depending on how you look of it) in having the code work for H < 13 with your compiler. The code is equally invalid for all values. With another compiler, it might fail for all values of H.
    quzah likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    memcpy is correct. You need to know the size of your VLA before you declare it. You could do something like this:
    Code:
    int size;
    printf( "How big is your array? " );
    fflush( stdout );
    scanf( "%d", &size );
    ... error check ...
    
    
    type yourarray[ size ];
    ... do stuff ...
    You cannot however change the size of the array once you've made it. It has to have a set size when you declare it, and it stays that size until it goes out of scope. So you could do something like this:
    Code:
    void foo( int size )
    {
        type array[ size ];
        ...
    }
    
    foo( 3 );
    foo( 100 );
    Not that this is necessarily what you want to do in this case, it's more an illustration of how VLAs work.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    So, basically I should declare the array to be bigger than H right from the outset?
    Thanks anyways

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Only if you know the initial size of H, and also know that it won't change.

    Otherwise, get the value first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation(Segmentation Fault) error
    By nirvana619 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 09:43 AM
  2. An Access Violation Segmentation Fault. Need Help ASAP
    By darknessfalls in forum C Programming
    Replies: 2
    Last Post: 08-22-2010, 06:56 AM
  3. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 12:43 PM
  4. Replies: 3
    Last Post: 07-17-2008, 09:45 AM
  5. access violation (segmentation fault)
    By MarlonDean in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2008, 06:02 AM

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