c++ - 在类中初始化动态数组的函数

标签 c++

作为练习,我正在将我的硕士论文用于模拟波传播的时域有限差分代码从 matlab 翻译到 c++,我遇到了以下问题。

我想创建一个对应于称为 cpml 的非物理吸收层的类。层的大小取决于模拟所需的参数,因此定义吸收层的阵列必须是动态的。

#ifndef fdtd_h
#define fdtd_h

#include <cmath>
#include <iostream>
#include <sstream>

using namespace std;

class cpml {

public:

int thickness;
int n_1, n_2, n_3;
double cut_off_freq;
double kappa_x_max, sigma_x_1_max, sigma_x_2_max, alpha_x_max;

double *kappa_x_tau_xy, *sigma_x_tau_xy, *alpha_x_tau_xy;

void set_cpml_parameters_tau_xy();

};

void cpml::set_cpml_parameters_tau_xy(){

double temp1[thickness], temp2[thickness], temp3[thickness];

for(int j = 1; j < thickness; j++){

    temp1[j] = 1 + kappa_x_max * pow((double)(thickness - j - 0.5) / (double)(thickness - 1), n_1);
    temp2[j] = sigma_x_1_max * pow((double)(thickness - j - 0.5) / (double)(thickness - 1), n_1 + n_2);
    temp3[j] = alpha_x_max * pow((double)(j - 0.5) / (double)(thickness - 1), n_3);


}

kappa_x_tau_xy = temp1;
sigma_x_tau_xy = temp2;

for(int i = 1; i < thickness; i++){

    cout << sigma_x_tau_xy[i] << endl;

}

alpha_x_tau_xy = temp3;

}

#endif /* fdtd_h */

当我在主函数中调用函数 cpml::set_cpml_parameters_tau_xy() 时,数组 sigma_x_tau_xy 的第一个值是正确的。但是,其他值不是。

#include "fdtd.h"

using namespace std;

int main() {

cpml cpml;

int cpml_thickness = 10;
cpml.thickness = cpml_thickness;

int n_1 = 3, n_2 = 0, n_3 = 3;
cpml.n_1 = n_1; cpml.n_2 = n_2; cpml.n_3 = n_3;

double cut_off_freq = 1;
cpml.cut_off_freq = cut_off_freq;

double kappa_x_max = 0;
double sigma_x_1_max = 0.8 * (n_1 + 1) / (sqrt(simulation_medium.mu/simulation_medium.rho) * simulation_grid.big_delta_x), sigma_x_2_max = 0.8 * (n_1 + 1) / (sqrt(simulation_medium.mu/simulation_medium.rho) * simulation_grid.big_delta_x);
double alpha_x_max = 2 * PI * cpml.cut_off_freq;

double kappa_y_max = 0;
double sigma_y_1_max = 0.8 * (n_1 + 1) / (sqrt(simulation_medium.mu/simulation_medium.rho) * simulation_grid.big_delta_y), sigma_y_2_max = 0.8 * (n_1 + 1) / (sqrt(simulation_medium.mu/simulation_medium.rho) * simulation_grid.big_delta_y);
double alpha_y_max = 2 * PI * cpml.cut_off_freq;

cpml.kappa_x_max = kappa_x_max; cpml.sigma_x_1_max = sigma_x_1_max; cpml.sigma_x_2_max = sigma_x_2_max; cpml.alpha_x_max = alpha_x_max;
cpml.kappa_y_max = kappa_y_max; cpml.sigma_y_1_max = sigma_y_1_max; cpml.sigma_y_2_max = sigma_y_2_max; cpml.alpha_y_max = alpha_y_max;

cpml.set_cpml_parameters_tau_xy();

for(int j = 1; j < cpml.thickness; j++){

    cout << *(cpml.sigma_x_tau_xy + j) << endl;

}

}

我做错了什么以及如何使类 cpml 的动态数组成员在主函数中调用时包含正确的值?

最佳答案

两个问题:其中较小的是您的程序在技术上不是有效的 C++ 程序,因为 C++ 没有 variable-length arrays (您的数组 temp1temp2temp3 是)。

更严重的问题是你保存了指向局部变量的指针。当函数返回时,局部变量超出范围并且不再存在。指向它们的指针将变得无效,使用这些指针将导致未定义的行为

这两个问题都可以通过使用 std::vector 轻松解决。而不是数组和指针。

关于c++ - 在类中初始化动态数组的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43230223/

相关文章:

c++ - 在 Shutdown() 方法而不是析构函数中清理

c++ - 如果 C++ 调用堆栈的顶部是 "???",地址全为零,这意味着什么?

c++ - 在屏幕尺寸变化时保持几何体完整

javascript - 浮点表示似乎可以正确地进行整数运算——为什么?

c++ - 包装数据结构

android - C++ Builder - 获取 Activity 的结果

c++ - 仅当函数的返回值等于 "Value"时,gdb 才能有条件地非交互地中断函数吗?

c++ - 如何在不要求任何链接文件的情况下使程序运行

c++ - 如何使生成的线程停止足够长的时间以使生成线程执行某些操作?

C++ 或 D : idiom to decouple classes without dynamic dispatch?