Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么数组[n]和数组[x],其中x=n返回不同的值?_C++_Arrays_Debugging - Fatal编程技术网

C++ 为什么数组[n]和数组[x],其中x=n返回不同的值?

C++ 为什么数组[n]和数组[x],其中x=n返回不同的值?,c++,arrays,debugging,C++,Arrays,Debugging,有人知道是什么原因导致数组[n]和数组[x](x=n)的读回值彼此不同吗 编辑:以下是一段可编译代码,用于说明我遇到的问题。如果运行以下代码,您将不会看到任何问题。我只是用它来描述我在原始程序中看到的问题,这是一个有100多个类的模拟器 #include <iostream> using namespace std; class MySimulatorImplementationBy100Classes; class MySimulator { public: enum

有人知道是什么原因导致数组[n]和数组[x](x=n)的读回值彼此不同吗

编辑:以下是一段可编译代码,用于说明我遇到的问题。如果运行以下代码,您将不会看到任何问题。我只是用它来描述我在原始程序中看到的问题,这是一个有100多个类的模拟器

#include <iostream>

using namespace std;

class MySimulatorImplementationBy100Classes;

class MySimulator {
public:
    enum SigList {
        SIG0,
        SIG1,
        SIG2,
        // ...
        SIG18=18,
        SIG19=19,
        SIG70=70,
        SIG80=80
    };

    void run() {}
private:
    MySimulatorImplementationBy100Classes *impl_;
};

int main() {
    MySimulator sim;
    // set up the simulation 

    sim.run();

    enum SigAnalysis {
        ANALYSIS0,
        ANALYSIS1,
        ANALYSIS2,
        ANALYSIS3,
        ANALYSIS4,
        ANALYSIS5,
        ANALYSIS6,
        NUM_ANALYSIS
    };

    const MySimulator::SigList signal_source[NUM_ANALYSIS] = {
        MySimulator::SIG18,
        MySimulator::SIG18,
        MySimulator::SIG18,
        MySimulator::SIG18,
        MySimulator::SIG70,
        MySimulator::SIG80,
        MySimulator::SIG19
    };

    for(int i=0; i<NUM_ANALYSIS; ++i) {
        cout <<signal_source[i]<<"\t" << MySimulator::SIG80 <<"\t" << signal_source[5] << "\t" << i << "\t";
        cout << &signal_source[i]<<"\t" << &signal_source[5]<< "\n";
    }

}
虽然我希望是这样

18              80      80      0       0xfffe1c90      0xfffe1ca4
18              80      80      1       0xfffe1c94      0xfffe1ca4
18              80      80      2       0xfffe1c98      0xfffe1ca4
18              80      80      3       0xfffe1c9c      0xfffe1ca4
70              80      80      4       0xfffe1ca0      0xfffe1ca4
80              80      80      5       0xfffe1ca4      0xfffe1ca4
19              80      80      6       0xfffe1ca8      0xfffe1ca4
我不知道为什么当I=5返回与
信号源[5]
不同的东西时
信号源[I]
信号源[5]
是相同的

此外,如果我在
cout

    sim.run();

    // dummy print
    cout << "dummy\n";

    enum SigAnalysis { //...     
有人知道这里会出什么问题吗


我使用的是gcc版本3.4.6。谢谢你的帮助

我能想到的唯一一件事是有什么东西正在破坏
信号源
(或预捕获
I

发布一个更完整的代码示例

更新:这是我的测试程序。它完全按照预期工作

enum SigList { 
    SIG0, 
    SIG1, 
    SIG2, 
    SIG18 = 18,
    SIG70 = 70,
    SIG80 = 80,
    SIG100 = 100
}; 
const SigList signal_source[7] = { 
    SIG1, 
    SIG1, 
    SIG1, 
    SIG1, 
    SIG18, 
    SIG70, 
    SIG80 
};

int _tmain(int argc, _TCHAR* argv[])
{
    cout << signal_source[0] << "\n";   // return 1 (SIG1) 
    cout << signal_source[1] << "\n";   // return 1 (SIG1) 

    for(int i=0; i<7; ++i) 
    { 
        cout << signal_source[i] << "\n"; 
    }

    return 0;
}
enum SigList{
SIG0,
信号1,
信号2,
SIG18=18,
SIG70=70,
SIG80=80,
SIG100=100
}; 
常量SigList信号源[7]={
信号1,
信号1,
信号1,
信号1,
信号18,
SIG70,
SIG80
};
int _tmain(int argc,_TCHAR*argv[]
{

cout我的第一个建议是在下运行程序。我发现,这些随机损坏问题可能是由于将未初始化的值传递给系统调用引起的


这里还有另一个想法:试着把随机打印语句放在所有地方,看看问题发生的地方是否发生了变化,那么这肯定是内存损坏的迹象。

也许当你到达for循环时,
信号源
是空的,因为你还没有初始化它,或者因为你在另一个函数中?

你能把它放进去吗这是一个可编译的示例吗?这是精简的代码吗?(顺便说一句,您可能需要使用更新版本的gcc)你上面的代码在VS2010中对我很有用,除了我必须填写由…指示的缺少的枚举。因此,请按照GMan的建议去做,以了解你的代码发生了什么。你可能还想打印我和你的结果,以验证你的索引没有做任何奇怪的事情。不确定这是否会有帮助,但值得一试。
dummy 
172402312               80      80      0       0xfffe1c90      0xfffe1ca4
172446752               80      80      1       0xfffe1c94      0xfffe1ca4
18              80      80      2       0xfffe1c98      0xfffe1ca4
18              80      80      3       0xfffe1c9c      0xfffe1ca4
70              80      80      4       0xfffe1ca0      0xfffe1ca4
80              80      80      5       0xfffe1ca4      0xfffe1ca4
19              80      80      6       0xfffe1ca8      0xfffe1ca4
enum SigList { 
    SIG0, 
    SIG1, 
    SIG2, 
    SIG18 = 18,
    SIG70 = 70,
    SIG80 = 80,
    SIG100 = 100
}; 
const SigList signal_source[7] = { 
    SIG1, 
    SIG1, 
    SIG1, 
    SIG1, 
    SIG18, 
    SIG70, 
    SIG80 
};

int _tmain(int argc, _TCHAR* argv[])
{
    cout << signal_source[0] << "\n";   // return 1 (SIG1) 
    cout << signal_source[1] << "\n";   // return 1 (SIG1) 

    for(int i=0; i<7; ++i) 
    { 
        cout << signal_source[i] << "\n"; 
    }

    return 0;
}