用C ++代码输出3D数组

时间:2015-07-10 23:02:46

标签: c++

我正在编写C ++代码并对3d数组有疑问:

double ***excit_pn;
excit_pn = new double **[num_y];
for (int i = 0; i < num_y; i++)
{
    excit_pn[i] = new double *[num_x];
    for (int j = 0; j < t.size(); j++)
    {
        excit_pn[i][j] = new double[num_y];
        for (int k = 0; k < num_y; k++)
        {
            excit_pn[i][j][k] = 0;
        }
    }
}

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        for (int k = 0; k < 5; k++)
        {
            cout << excit_pn[i][j][k];
        }
        cout << endl;
    }
    cout << endl;
}
上面的

代码是我的代码的一部分。 我想测试数组excit_pn的值。当我运行代码时,运行结果都是4.26329e-312而不是0.但是当我添加

cout<<excit_pn[i][j][k];

行后:

excit_pn[i][j][k] = 0;

,运行结果为0。

我的第二个问题是:

for (int i = (num_y - num_el / 2); i < (num_y - num_el / 2)+num_el; i++)
{
    for (int j = 0; j < ts.size(); j++)
    {
        for (int k = (num_x - num_el / 2); k < (num_x - num_el / 2)+num_el; k++)
        {
            excit_pn[i][j][k] = excit_ptn[i - (num_y - num_el / 2)][j][k - (num_y - num_el / 2)];
        }
    }
}

此部分出错:0xC0000005:访问冲突读取位置0xFFFFFFFFFFFFFFFF。 excit_ptn已在上面定义:

ts_n = new double **[num_el];  //number of elements is num_x while the nonzero element is num_el
ye_n = new double **[num_el];
excit_ptn = new double **[num_el];
for (int i = 0; i < num_el; i++)
{
    ts_n[i] = new double *[ts.size()];
    ye_n[i] = new double *[ts.size()];
    excit_ptn[i] = new double *[ts.size()];
    for (int j = 0; j < ts.size(); j++)
    {
        ts_n[i][j] = new double[num_el];
        ye_n[i][j] = new double[num_el];
        excit_ptn[i][j] = new double[num_el];
        for (int k = 0; k < num_el; k++)
        {
            ts_n[i][j][k] = ts[j] + con_delay[i][k];
            ye_n[i][j][k] = exp(-ts_n[i][j][k] * ts_n[i][j][k] / (2 * tv));
            excit_ptn[i][j][k] = p0* ye_n[i][j][k] * cos(2 * PI*fc*ts_n[i][j][k]);
        }
    }
}

非常感谢

1 个答案:

答案 0 :(得分:0)

在中间循环分配中,你有t.size()的循环结束:

var list = Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly).
    SelectMany(sampleDir => Directory.EnumerateDirectories(sampleDir, "*", SearchOption.TopDirectoryOnly)).
    SelectMany(textdir => Directory.EnumerateFiles(textdir, "grouptest.log", SearchOption.TopDirectoryOnly));

但您使用变量num_x进行分配。

for (int j = 0; j < t.size(); j++)

这两个数量应该相同。

在您的打印循环中,您再次使用不同的尺寸。为了更安全,您应该尝试在调试时使用相同的尺寸。