user3482695
user3482695

Reputation: 13

C segmentation fault: 11

#include <stdio.h>
int main()
{
    char memf[10000];
    int memlen, vn , vw,vh,fattime , posit , speed , interval , nattacks,time=0,qa=0,ninit=0,elxan;
    scanf("%d\n",&memlen);
    scanf("%d\n",&vn);
    scanf("%d %d %d %d %d %d %d",&vw,&vh,&fattime,&posit,&speed,&interval,&nattacks);

        while(nattacks>ninit)
        {
            if(time==fattime)
            {
                for(elxan=posit;elxan<=posit+vw;elxan=posit++)
                {
                    memf[elxan]=vh;
                }
                posit=posit+speed;
                time++;
                ninit++;

            }
            else if(time>fattime)
             {
                for(qa=0;qa<100000;qa++)
                {
                   if(ninit==nattacks)
                       break;
                   else if(qa%interval==0)
                   {
                       for(elxan=posit;elxan<=posit+vw;elxan=posit++)
                       {
                           memf[elxan]=vh;
                       }
                       posit=posit+speed;
                       time++;
                       ninit++;
                   }


                   else
                       posit=posit+speed;
                  }
             }
             else
             {  time++;
                posit=posit+speed;

             }
        }
    /*for(px=0;px<=memlen;px++)
    {
        if(memf[px]=='0')
            memf[px]=1;
    }*/



    printf("%s",memf);
    return 0;
}

I get segmentation fault:11 while executing this code. I know it has something to do with arrays. What is the mistake?

Upvotes: 0

Views: 102

Answers (2)

RBerteig
RBerteig

Reputation: 43326

I think this loop is almost guaranteed to run away and access memory outside of the array memf, even assuming that the entered initial values are reasonable.

for(elxan=posit;elxan<=posit+vw;elxan=posit++) {  
    memf[elxan]=vh; 
}

Each iteration tests against posit+vw, but each iteration also increments posit while keeping elxan set to posit's previous value, which unless pathological values for posit and vw are entered, is guaranteed to make the condition always true, and the loop will not terminate within sizeof(memf) iterations.

Once elxan is large enough, accessing memory outside of the declared size of the array is going to cause trouble. While in general you have invoked "undefined behavior" after which nearly anything can happen, the specific symptom of a segmentation fault is very consistent with what we would expect to see eventually from overrunning an array bounds.

Upvotes: 2

S&#248;ren Debois
S&#248;ren Debois

Reputation: 5688

Here's how to deal with such an error:

First, add #include <assert.h>. Then, whenever you read from or assign to an array, test first that you are indexing within bounds. E.g., this line:

memf[elxan]=vh;

Should be changed to these lines:

assert(0 <= elxan && elxan < 10000);
memf[elxan]=vh;

This will help you pin-point the error.


You'll notice that you usually don't see so many asserts in other C-programs. That's ok; once you get used to writing C, you'll find you can safely let some of them go. For starters, I think you'll find them very helpful.

Upvotes: 2

Related Questions