【问题标题】:Pointer to a Matrix is throwing exception in c++ [duplicate]指向矩阵的指针在 C++ 中引发异常 [重复]
【发布时间】:2012-11-19 22:36:38
【问题描述】:

可能重复:
How do I work with dynamic multi-dimensional arrays in C?
Static Matrix Casting to a pointer

我的这段代码在行抛出异常

           resMat[i][j] = matp[i][j];

// 代码从这里开始

    #include <iostream>
    #define MOD 1000000007
    #define LL long long
    using namespace std;

    int mat[6][64][64],mat2[2][2], mat4[4][4], mat8[8][8], mat16[16][16], mat32[32][32], mat64[64][64];
    int **point[6];
    int resMat[64][64];

    void fooMatrix(int **matp, int size)
    {
        int i,j;
        // find canFollow of all matrixes   
        for(i=0;i<size;++i)
        {
            for(j=0;j<size;++j)
            {
      // throwing exception here
                resMat[i][j] = matp[i][j];
            }
        }
    }



    int main()
    {
        point[0] = (int **)mat2;
        point[1] = (int **)mat4;
        point[2] = (int **)mat8;
        point[3] = (int **)mat16;
        point[4] = (int **)mat32;
        point[5] = (int **)mat64;

        LL a,b,res;
        scanf("%lld %lld",&a,&b);
        fooMatrix(point[a-1],1<<a);
                    return 0;
    }

我想在我的函数 fooMatrix 中处理不同大小的 int 矩阵,比如将它存储在 resMat 中。帮我解决这个问题。

我在 windows 中使用 DevC++(g++ 编译器)。

【问题讨论】:

  • 你在哪里调用函数 fooMatrix?
  • 你所有的演员都错了。删除它们。
  • 如果你转储#include 并使用命名空间std;对于#include ,当编译为c而不是c++时它会做什么?
  • 如果您将mat2mat4 等...作为matp 传递,那么它不起作用,因为这些类型(int**int[][])描述不同内存中的数据布局。也就是说,无论任何人告诉你什么,数组和指针都是一回事。顺便说一句——你必须强制转换以进行分配 point[2] = (int **)mat8; 的事实是一个提示:你已经破坏了类型系统。
  • 已有many question of the years on the passing of multidimensional array to functionsa bunch concentrating on c 也有,如果您必须使用这样的原始数组和指针,它可能会有更有用的答案。

标签: c++ pointers exception matrix


【解决方案1】:

从上面的 cmets 和链接中我了解到:

矩阵表示[][]和指针表示**是不同的。编译器给我警告。

二维矩阵不是指针数组。所以看看下面的代码

    #include <stdio.h>
#define MOD 1000000007
#define LL long long

int mat[6][64][64],mat2[2][2], mat4[4][4], mat8[8][8], mat16[16][16], mat32[32][32], mat64[64][64];

// see this is array of single pointer
int *point[6];

int resMat[64][64];

void fooMatrix(int *matp, int size)
{
    int i,j;
    // find canFollow of all matrixes   
    for(i=0;i<size;++i)
    {
        for(j=0;j<size;++j)
        {
               // this is how we would access the matrix.
            resMat[i][j] = matp[i*size+j];
        }
    }
}

int main()
{
    point[0] = &mat2[0][0];
    point[1] = &mat4[0][0];
    point[2] = &mat8[0][0];
    point[3] = &mat16[0][0];
    point[4] = &mat32[0][0];
    point[5] = &mat64[0][0];

    LL a,b,res;
    scanf("%lld %lld",&a,&b);
    fooMatrix(point[a-1],1<<a);
    return 0;
}

【讨论】:

  • 也可以在 c++ 中工作。将 stdio.h 包含更改为 iostream 的包含
【解决方案2】:

您将不得不使用动态分配的指向数组的指针数组,而不是使用矩阵。

您可以将文件顶部的声明替换为以下内容:

int** newMat(int a, int b){
  int** result = new int*[a];
  for(int i=0; i<a; ++i)
    result[i] = new int[b];
  return result;
}

int** mat2 = newMat(2,2);
int** mat4 = newMat(4,4);
int** mat8 = newMat(8,8);
int** mat16 = newMat(16,16);
int** mat32 = newMat(32,32);
int** mat64 = newMat(64,64);
int*** point = new int**[6];
int** resMat= newMat(64,64);

然后将main顶部的分配更改为:

  point[0] = mat2;
  point[1] = mat4;
  point[2] = mat8;
  point[3] = mat16;
  point[4] = mat32;
  point[5] = mat64;

【讨论】:

    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多