c++ - Stack around variable is corrupted 错误

标签 c++

我正在编写以下代码,但出现了 Stack around variable is corrupted 错误。我检查过类似的帖子,但无法解决问题。我对 C++ 也很陌生。这是我的代码。

///CLASS DEFINITION
class Trellis{

    private:
        int m;
        int Nstates;
        int StateTransition[];

    public:
        Trellis(); 
        Trellis(int M);
};

这里是方法的定义

Trellis::Trellis(int M){
    m = M;
    Nstates = pow(2, M - 1);

    for (int i = 0; i < Nstates; i++){
        StateTransition[i] = i;
    }
}

主要内容() {

Trellis Tu = Trellis(3);

返回0;

我得到的错误是运行时检查失败 #2-Stack around variuble 'Tu' was corrupted;

最佳答案

你没有为 StateTransition 分配任何内存,你想要这样的东西:

StateTransition = new int[Nstates];

在 ctor 中的 for 循环之前。

关于c++ - Stack around variable is corrupted 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24759937/

相关文章:

c++ - c++ 中不同类文件中的主要方法?

c++ - 字节顺序会导致读写操作冲突吗?

c++ - 我是否必须明确地销毁对象才能

c++ - 如何设置、清除和切换单个位?

c++ 数组元素的别名

c++ - OpenMP 开销计算

c++ - 如何将 USB 端口的数字信号加载到内存中?

c++ - 如何使用 wcout 显示 L"أَبْ♬َدِيوّ

c++ - 如何归一化一维 FFT 输出

c++ - NULL 指针比较失败