[英]Xcode __pthread_kill + 20
我正在嘗試使用大型數組運行簡單的命令行應用程序,如果我嘗試循環遍歷該數組的30行,它將起作用,但是當我將其增加到1000時,它崩潰了,並在控制台中顯示(lldb)。 在線程1下,我有__pthread_kill和__stack_chk_fail。 我的代碼:
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])
^^^
因此, x
從0到50(含)。 這意味着mains[0]
必須具有51個元素。
(如果你不明白為什么,想象一下,如果x
從0走到4
包容性。這將意味着你需要5
元, 0
, 1
, 2
, 3
,和4
。所以,如果你從0迭代,以n
包容性,你需要n+1
元素。)
int mains [2][50]={1,2,3,
^^
糟糕,它只有50個。
當您增加局部變量的大小時,您將耗盡堆棧空間:
__stack_chk_fail-在堆棧溢出的情況下終止函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.