You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS下C++矩阵螺旋遍历代码触发Segmentation fault求错误定位

问题说明

C++编程初学者在macOS设备的VS Code环境运行矩阵螺旋遍历代码时触发Segmentation fault(段错误),代码无编译报错但运行时崩溃,无法定位问题,原实现代码如下:

#include<iostream>
using namespace std;
# define n 4
#define m 4
 void spiral(int r, int c,int arr[n][m])
 {
     int last_row=r-1 , last_col =c-1;
     int left=0, right = last_col;
     int top=0, bottom = last_row;
     

     while(top<=bottom && left<=right)
     {
         for (int i=left;i<=right ;i++){
             cout<<arr[top][i]<<" ";
         }
         top++;

         for(int i=top; i<=bottom;i++){
             cout<<arr[i][right]<<" ";
         }
         right--;
        if(top<=bottom){
         for(int i=right; i>=left;i++){
             cout<<arr[i][bottom]<<" ";
         }
         bottom--;
        }

         if(left<=right){
             for(int i=bottom; i>=top;i++){
             cout<<arr[i][left]<<" ";
         }
         left++;
         }


     }
   
 }
 int  main(){
     
     /*int arr[n][m];
     for(int i=0;i<n;i++){
         for(int j=0;j<m;j++){
             cin>>arr[i][j];
         }
     }*/
     int arr[n][m]={
         {1,2,3,4},
         {1,2,3,4},
         {5,6,7,8},
         {5,6,7,8},
     };
     spiral(n,m,arr);
     return 0;
 
 }
故障原因

段错误由两处逻辑错误共同导致,全部出现在从右向左遍历矩阵底部行的代码块中:

  • 循环步进方向错误:对应循环的终止条件为i >= left,但循环变量步进写为i++,会导致i持续增大永远无法满足退出条件,触发死循环;当i的值超出数组索引的合法范围(0~3)时,就会访问未分配给程序的内存地址,直接触发段错误。
  • 数组索引顺序写反:遍历底部固定行时,行索引应该固定为当前的边界值bottom,列索引随循环变量i变化,原代码写为arr[i][bottom],混淆了行、列索引的位置,进一步加速了内存越界。

另外代码中定义的last_row、last_col属于冗余变量,全程未被使用,虽然不会引发报错,但属于不必要的代码。

修复后代码

修正上述两处错误、移除冗余变量后的可正常运行代码如下:

#include<iostream>
using namespace std;
#define n 4
#define m 4

void spiral(int r, int c, int arr[n][m])
{
    int left = 0, right = c - 1;
    int top = 0, bottom = r - 1;
    
    while(top <= bottom && left <= right)
    {
        // 从左到右遍历顶部行
        for (int i = left; i <= right; i++){
            cout << arr[top][i] << " ";
        }
        top++;

        // 从上到下遍历右侧列
        for(int i = top; i <= bottom; i++){
            cout << arr[i][right] << " ";
        }
        right--;

        // 从右到左遍历底部行,边界判断避免单行场景重复遍历
        if(top <= bottom){
            // 修复:步进改为i--,索引修正为arr[bottom][i]
            for(int i = right; i >= left; i--){
                cout << arr[bottom][i] << " ";
            }
            bottom--;
        }

        // 从下到上遍历左侧列,边界判断避免单列场景重复遍历
        if(left <= right){
            for(int i = bottom; i >= top; i--){
                cout << arr[i][left] << " ";
            }
            left++;
        }
    }
}

int main(){
    int arr[n][m] = {
        {1,2,3,4},
        {1,2,3,4},
        {5,6,7,8},
        {5,6,7,8},
    };
    spiral(n, m, arr);
    return 0;
}

修复后代码运行输出的螺旋顺序结果为:1 2 3 4 4 8 8 7 6 5 5 1 2 3 7 6,符合预期。

内容的提问来源于stack exchange,提问作者Garima Gill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 05:39:18