【问题标题】:Why do I get a segmentation fault (core dumped) when I try to allocate a 4D array?当我尝试分配 4D 数组时,为什么会出现分段错误(核心转储)?
【发布时间】:2009-03-12 06:25:59
【问题描述】:

我正在尝试分配一个大型 4D 矩阵,但我想动态分配。当我刚刚创建静态矩阵时,一切正常,所以我知道我现在有足够的内存。但是,当我尝试动态实现相同的东西时,每当我进入第三个维度并且我需要进入第四个维度时,它就会中断!谁能告诉我为什么这段代码不起作用?

#include <iostream>

using namespace std;

static const int time1 = 7;
static const int tlat = 15;
static const int tlon = 17;
static const int outlev = 3;  

int main(void)
{
    //allocate four dimensional dataIn
    int ****dataIn;
    dataIn = new int ***[time1];
    if (dataIn == NULL) { return 1; }

    for(int i = 0 ; i < time1 ; i++) { 
        dataIn[i] = new int **[tlat];
        if (dataIn[i] == NULL) { return 1; }

        for(int j = 0 ; j < tlat ; j++) {
            dataIn[i][j] = new int *[tlon];
            if (dataIn[i][j] == NULL) { return 1; }

            for(int k = 0 ; k < tlon ; k++) {
                dataIn[i][j][k] = new int[outlev];
                if (dataIn[i][j][k] == NULL) { return 1; }
            }
        }
    }
    //there is more code that happens here to add values to dataIn
    //and eventually output it but I know all of that works        
    return 0;
}

我在这段代码上尝试了许多不同的变体,甚至使用 malloc 代替 new,但我无法让它工作。任何帮助将不胜感激。

【问题讨论】:

  • 这里不会崩溃。也许实际代码块会有所帮助。
  • 我编译了您发布的代码,它在 VS2005 中运行而没有崩溃。请更具体地说明您的问题。
  • 在下面的评论中,您指出错误在线:'dataIn[i][j] ='。 i 和 j 的值是多少。返回的指针的值是多少等。如果您没有调试器(难以理解),那么您可以通过放置显示状态的打印语句来以老式方式进行。

标签: c++ dynamic matrix segmentation-fault


【解决方案1】:

您是否在调试器中运行过它?在我看来,代码看起来不错,但调试器会告诉您它在哪里崩溃,仅此一项就足以帮助您修复它

【讨论】:

  • 代码编译得很好,除了我的编译器我没有调试器。由于我自己的调试只是尝试了很多事情,我知道它在尝试分配 dataIn[i][j] 的行上中断了。由于某种原因,该指针可能不好,但我不知道为什么或如何修复它。
  • err,你在哪个平台上没有调试器?
  • 一个调试器会改变你的生活
【解决方案2】:

您最好将所有内存分配到一个平面数组中,然后自己计算索引。将整个事物包装在一个对象中以进行封装。

class Matrix {
private:
        int* data;        
        int[] sizes;
        int nDimensions;

public:
        // allocates the data pointer and copies the other parameters
        Matrix(int[] sizes, int nDimensions); 

        // frees the data and sizes arrays
        ~Matrix();

        // calculates the cell position and returns it
        int getCell(int[] coordinates);

        // calcultes the cell position and sets its value
        void setCell(int[] coordinates, int value);

private:
        // used by getCell and setCell, calculates the cell's 
        // location in the data array
        size_t calculateCellPosition(int[] coordinates);
};

【讨论】:

  • 这种方法还将提供速度更快的优势。尽管并非所有情况都要求代码速度快,但这仍然是一个好习惯。
【解决方案3】:

它在我的 Linux 机器上编译并运行良好。

顺便说一句,对于标准 C++,new 在无法分配内存时会抛出 std::bad_alloc 异常(而不是返回 NULL)。因此,捕获该异常而不是测试 NULL 指针可能是值得的。

【讨论】:

    【解决方案4】:

    正如 cmeerw 所指出的,在 std-c++ 中不需要针对 NULL 或 0 进行测试。

    如果您可以在获得 segv 的确切位置添加评论,将会有所帮助。

    另外:您使用的是哪个编译器以及在哪个操作系统上?

    【讨论】:

      【解决方案5】:

      查看boost::multiarray,它完全符合您的要求,只是通过仅执行单个堆分配更有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多