C语言 中止 C 中的陷阱 6 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26431147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:28:47  来源:igfitidea点击:

Abort trap 6 error in C

carraysloopsabort

提问by user1753491

I have this code:

我有这个代码:

void drawInitialNim(int num1, int num2, int num3)
{
    int board[2][50]; //make an array with 3 columns
    int i; // i, j, k are loop counters
    int j;
    int k;

    for(i=0;i<num1+1;i++)      //fill the array with rocks, or 'O'
        board[0][i] = 'O';     //for example, if num1 is 5, fill the first row with 5 rocks
    for (i=0; i<num2+1; i++)
        board[1][i] = 'O';
    for (i=0; i<num3+1; i++)
        board[2][i] = 'O';

    for (j=0; j<2;j++) {       //print the array
      for (k=0; k<50;k++) {
         printf("%d",board[j][k]);
      }
    }
   return;
}

int main()
{
    int numRock1,numRock2,numRock3;
    numRock1 = 0;
    numRock2 = 0;
    numRock3 = 0; 
    printf("Welcome to Nim!\n");
    printf("Enter the number of rocks in each row: ");
    scanf("%d %d %d", &numRock1, &numRock2, &numRock3);
    drawInitialNim(numRock1, numRock2, numRock3); //call the function

    return 0;
}

When I compile this with gcc, it is fine. When I run the file, I get the abort trap 6 error after entering the values.

当我用 gcc 编译它时,它很好。当我运行该文件时,我在输入值后收到 abort trap 6 错误。

I have looked at other posts about this error, and they don't help me.

我已经查看了有关此错误的其他帖子,但它们对我没有帮助。

采纳答案by BLUEPIXY

Try this:

尝试这个:

void drawInitialNim(int num1, int num2, int num3){
    int board[3][50] = {0}; // This is a local variable. It is not possible to use it after returning from this function. 

    int i, j, k;

    for(i=0; i<num1; i++)
        board[0][i] = 'O';
    for(i=0; i<num2; i++)
        board[1][i] = 'O';
    for(i=0; i<num3; i++)
        board[2][i] = 'O';

    for (j=0; j<3;j++) {
        for (k=0; k<50; k++) {
            if(board[j][k] != 0)
                printf("%c", board[j][k]);
        }
        printf("\n");
    }
}

回答by ryyker

You are writing to memory you do not own:

您正在写入不属于您的内存:

int board[2][50]; //make an array with 3 columns  (wrong)
                  //(actually makes an array with only two 'columns')
...
for (i=0; i<num3+1; i++)
    board[2][i] = 'O';
          ^

Change this line:

改变这一行:

int board[2][50]; //array with 2 columns (legal indices [0-1][0-49])
          ^

To:

到:

int board[3][50]; //array with 3 columns (legal indices [0-2][0-49])
          ^

When creating an array, the value used to initialize: [3]indicates array size.
However, when accessing existing array elements, index values are zero based.

创建数组时,用于初始化的值:[3]表示数组大小。
但是,在访问现有数组元素时,索引值是从零开始的

For an array created: int board[3][50];
Legal indices are board[0][0]...board[2][49]

对于创建的数组: int board[3][50];
合法索引为 board[0][0]...board[2][49]

EDITTo address bad output comment and initialization comment

编辑以解决错误的输出注释和初始化注释

add an additional "\n" for formatting output:

添加额外的 "\n" 用于格式化输出:

Change:

改变:

  ...
  for (k=0; k<50;k++) {
     printf("%d",board[j][k]);
  }
 }

       ...

To:

到:

  ...
  for (k=0; k<50;k++) {
     printf("%d",board[j][k]);
  }
  printf("\n");//at the end of every row, print a new line
}
...  

Initializeboard variable:

初始化板变量:

int board[3][50] = {0};//initialize all elements to zero

( array initialization discussion...)

数组初始化讨论...