简体   繁体   English

Xcode __pthread_kill + 20

[英]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. 我正在尝试使用大型数组运行简单的命令行应用程序,如果我尝试循环遍历该数组的30行,它将起作用,但是当我将其增加到1000时,它崩溃了,并在控制台中显示(lldb)。 Under thread 1 I have __pthread_kill , __stack_chk_fail . 在线程1下,我有__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. 因此, x从0到50(含)。 That means mains[0] has to have 51 elements. 这意味着mains[0]必须具有51个元素。

(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.) (如果你不明白为什么,想象一下,如果x从0走到4包容性。这将意味着你需要5元, 0123 ,和4 。所以,如果你从0迭代,以n包容性,你需要n+1元素。)

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

Oops, it only has 50. 糟糕,它只有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 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 __stack_chk_fail-在堆栈溢出的情况下终止函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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