简体   繁体   中英

Xcode __pthread_kill + 20

I am trying to run simple command line application with large array, it works if I try looping through 30 rows of the array, but when I increase it to 1000 it crashes, showing (lldb) in the console. Under thread 1 I have __pthread_kill , __stack_chk_fail . My code:

int main(int argc, const char * argv[]) {


int i;
int j;
int x;


int mains [2][50]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
    31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
int stars [2][11]={1,2,3,4,5,6,7,8,9,10,11};

int results [1000][7]={

    {27,29,37,39,49,2,4}, //03.04.2015

    {8,20,24,28,49,8,9},

 // rest of array not relevant to the problem

    {16,29,32,36,41,7,9},

};



for (i=0; i<1000; i++)
{
    for (j=0; j<5; j++)
    {
        for (x=0; x<51; x++)
        {
            if (results[i][j]==mains[0][x])
            {
                mains[1][x]++;
            }
        }

    }
}

for (i=0; i<1000; i++)
{
    for (j=5; j<7; j++)
    {
        for (x=0; x<12; x++)
        {
            if (results[i][j]==stars[0][x])
            {
                stars[1][x]++;
            }
        }

    }
}



for (i=0; i<49; i++)      //array sorting
{
    for (j=i+1; j<50; j++)
    {
        if (mains[1][i]>mains[1][j])
        {
            std::swap(mains[0][i],mains[0][j]);
            std::swap(mains[1][i],mains[1][j]);            }
    }

}

for (i=0; i<10; i++)      //array sorting
{
    for (j=i+1; j<11; j++)
    {
        if (stars[1][i]>stars[1][j])
        {
            std::swap(stars[0][i],stars[0][j]);
            std::swap(stars[1][i],stars[1][j]);            }
    }

}




printf("Mains        Stars:\n\n");
for (i=0; i<5; i++)
{
    printf("%d ",mains[0][i]);
}

for (i=0; i<2; i++)
{
    printf("%d ",stars[0][i]);
}


return 0;
}
    for (x=0; x<51; x++)
              ^^^^
    {
        if (results[i][j]==mains[0][x])
                                   ^^^ 

So, x goes from 0 to 50 inclusive. That means mains[0] has to have 51 elements.

(If you don't see why, imagine if x went from 0 to 4 inclusive. That would mean you need 5 elements, 0 , 1 , 2 , 3 , and 4 . So if you iterate from 0 to n inclusive, you need n+1 elements.)

int mains [2][50]={1,2,3,
              ^^

Oops, it only has 50.

When you increased the size of your local variable, you ran out of stack space:

http://refspecs.linux-foundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/libc---stack-chk-fail-1.html

__stack_chk_fail -- terminate a function in case of stack overflow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM