当我尝试将字符存储在2D Malloc数组中时,它说“分离故障(核心倾倒)”

发布于 2025-01-21 06:40:06 字数 1611 浏览 2 评论 0原文

我正在尝试创建一个简单的蛇游戏。这是其中的第一部分,根据用户的命令行输入,使用“ ”显示地图的边界。但是,当我尝试创建malloc数组并储存''时,它会崩溃,说“分割故障(核心倾倒)”。请帮助我找出此错误并修复我的代码。我是C编程的新手。

         #include<stdio.h>
         #include<stdlib.h>

   void initializeSnake(char** pArray, int pRows, int pColumns, int pSnakeLength);
   void print(char** pArray, int pRows, int pColumns);


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

 int rows=atoi(argv[1]);
int columns=atoi(argv[2]);
int snakeLength=atoi(argv[3]);

char** mapArray;
mapArray=(char**)malloc(rows*sizeof(char*));
for(int i=0;i<rows;i++)
mapArray[i]=(char*)malloc(columns*sizeof(char*));

  initializeSnake(mapArray, rows, columns, snakeLength);
  print(mapArray,rows,columns);

  printf("Rows: %s\n",argv[1]);
  printf("Columns: %s\n",argv[2]);
  printf("Snake Length: %s\n",argv[3]);



  return 0;
    }
   void initializeSnake(char** pArray, int pRows, int pColumns, int pSnakeLength)
 {
    for(int m=0;m<pRows+2;m++)
   {

for(int n=0;n<pColumns+2;n++)
{
  if(m==0 || m==pRows+1)
    pArray[m][n]=42;
  else if(n==0 || n==pColumns+1)
    pArray[m][n]=42;
  else if(m!=1)
    pArray[m][n]=' ';

    if(m==1)
    {
      if(n==0)
        pArray[m][n]='#';
      else if(n>0 && n<pSnakeLength)
        pArray[m][n]='-';
      else if(n==pSnakeLength)
        pArray[m][n]='>';
      else if(n<pColumns)
        pArray[m][n]=' ';

    }

}
   }

 }

  void print(char** pArray, int pRows, int pColumns)
   {
   for(int m=0;m<pRows;m++)
   {
    for(int n=0;n<pColumns;n++)
    {
     printf("%c",pArray[m][n]);
    }
printf("\n");
 }
 }

I am trying to create a simple snake game. This is first part of it, to display the borders of the map using '' according to user's command line input. However, when I try to create a malloc array and store '' accordingly, it crashes saying "Segmentation fault (core dumped)". Please help me figure out this error and fix my code. I am new to C programming.

         #include<stdio.h>
         #include<stdlib.h>

   void initializeSnake(char** pArray, int pRows, int pColumns, int pSnakeLength);
   void print(char** pArray, int pRows, int pColumns);


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

 int rows=atoi(argv[1]);
int columns=atoi(argv[2]);
int snakeLength=atoi(argv[3]);

char** mapArray;
mapArray=(char**)malloc(rows*sizeof(char*));
for(int i=0;i<rows;i++)
mapArray[i]=(char*)malloc(columns*sizeof(char*));

  initializeSnake(mapArray, rows, columns, snakeLength);
  print(mapArray,rows,columns);

  printf("Rows: %s\n",argv[1]);
  printf("Columns: %s\n",argv[2]);
  printf("Snake Length: %s\n",argv[3]);



  return 0;
    }
   void initializeSnake(char** pArray, int pRows, int pColumns, int pSnakeLength)
 {
    for(int m=0;m<pRows+2;m++)
   {

for(int n=0;n<pColumns+2;n++)
{
  if(m==0 || m==pRows+1)
    pArray[m][n]=42;
  else if(n==0 || n==pColumns+1)
    pArray[m][n]=42;
  else if(m!=1)
    pArray[m][n]=' ';

    if(m==1)
    {
      if(n==0)
        pArray[m][n]='#';
      else if(n>0 && n<pSnakeLength)
        pArray[m][n]='-';
      else if(n==pSnakeLength)
        pArray[m][n]='>';
      else if(n<pColumns)
        pArray[m][n]=' ';

    }

}
   }

 }

  void print(char** pArray, int pRows, int pColumns)
   {
   for(int m=0;m<pRows;m++)
   {
    for(int n=0;n<pColumns;n++)
    {
     printf("%c",pArray[m][n]);
    }
printf("\n");
 }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2025-01-28 06:40:06

您故意将您分配给

您创建的尺寸行的数组之外 * cols * cols

char** mapArray;
mapArray = (char**)malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++)
    mapArray[i] = (char*)malloc(columns * sizeof(char*));

通过此信息将其传递给初始化,

initializeSnake(mapArray, rows, columns, snakeLength);

然后超越该功能的界限,

for (int n = 0; n < pColumns + 2; n++)
-----------------------------^^^

最终您尝试访问比创建的2列,而行也是如此。我不知道该+2的意图,因此无法提出解决方案。但是,正如您发现的那样,您无法做自己当前正在做的事情。

You deliberately go outside the array you allocated

You create with size rows * cols

char** mapArray;
mapArray = (char**)malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++)
    mapArray[i] = (char*)malloc(columns * sizeof(char*));

Pass this info to initializeSnake

initializeSnake(mapArray, rows, columns, snakeLength);

Then go beyond the bounds in that function

for (int n = 0; n < pColumns + 2; n++)
-----------------------------^^^

You end up trying to access 2 more columns than you created, same for rows. I do not know the intent of that +2 so cannot propose a solution. But, as you have discovered, you cannot do what you are currently doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文