开发者

Why do array[n] and array[x] where x=n return different values?

Does anyone know what may cause the read-back values of array[n] and array[x] (x=n) different from each other?

EDIT: Following is a compilable code to illustrate the problem I encountered. If you run the following code, you won't see any problem. I am just using it to describe the problem I saw in my original program which is a simulator with 100+ classes.

#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";
    }

}

In my program, the outputs are

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
173068832               80      80      5       0xfffe1ca4      0xfffe1ca4
168047112               80      80      6       0xfffe1ca8      0xfffe1ca4

While I expect it to be

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 don't know why signal_source[i] when i=5 returns something different from 开发者_运维知识库signal_source[5], while &signal_source[i] and &signal_source[5] are identical.

Furthermore, if I add some dummy codes somewhere before the for loop of cout

    sim.run();

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

    enum SigAnalysis { //...     

The results will change:

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

Does anyone have a clue what may go wrong here?

I am using gcc ver 3.4.6. Thanks for the help!


The only thing I can think of is that something is corrupting signal_source (or prehaps i).

Post a more complete code sample.

UPDATE: Here is my test program. It worked exactly as expected.

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;
}


My first suggestion would be to run the program under valgrind. I've found that these kind of random corruption issues can be caused by passing uninitialized values to system calls.

Here's another idea: try putting random print statements all over the place and see if that changes where the problem occurs, then it's a sure sign of memory corruption.


Perhaps when you reach the for loop, signal_source is empty, because you didn't initialize it yet, or because you are in another function?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜