阵列没有溢出时的损坏

时间:2015-03-23 22:50:05

标签: c++ arrays corruption

我遇到了Q_List[2].Status的腐败现象,而且在for循环之后它会腐蚀到Edge。我之前已经设置为普通枚举,并且作为int的损坏值出现为256.将其更改为枚举类,我仍然遇到问题。我看不出它是如何溢出数组的,因为它在for循环的边界上没有超过2并且我将其设置为2 + 1,但是如果我将其更改为:

bool Available[Max_Energy + 2][Max_Energy + 2][...

然后它不会腐败,所以就是这样。

Main.cpp的

Q_List[Current_Q].Fill_Available_Fns_Array(Q_List[Adjacent_Q_ID], Right, &Q_List[2]);

Q.h

bool Available[Max_Energy + 1][Max_Energy + 1][Right][Longest_Range + 1];

status Get_Status() { // Transcendent returns 
    //scriptfile << "Call Get_Status() = " << Status << ".\n";
    if (Status == status::Present) {
        return Status;
    }
    else if (Status == status::Transcendent){
        return Status;
    }
    else if (Status == status::Edge){
        return Status;
    }
    else
    {
        return status::Edge;
    }
};

void Fill_Available_Fns_Array(Quanta Second_Q, Handed Side, Quanta * Debug) {
    Second_Q_Effective_Energy = Max_Energy;
    scriptfile << "Here???? this = " << this << " with energy = " << (*this).Get_Energy() << "\n";
    for (Q_energy_count = 1; Q_energy_count <= (*this).Get_Energy(); Q_energy_count++){
        for (Second_Q_energy_count = 1; Second_Q_energy_count <= Second_Q_Effective_Energy; Second_Q_energy_count++){
            for (Slot_C = 1; Slot_C <= Longest_Range; Slot_C++){
                scriptfile << "X" << Q_energy_count << Second_Q_energy_count << Side << Slot_C;
                Available[Q_energy_count][Second_Q_energy_count][Side][Slot_C] = true;
                if ((*Debug).Get_Status() == status::Edge)
                {
                    scriptfile << "E\n";
                }
                else if ((*Debug).Get_Status() == status::Present)
                {
                    scriptfile << "P\n";
                }
                else if ((*Debug).Get_Status() == status::Transcendent)
                {
                    scriptfile << "T\n";
                }
                else
                    scriptfile << "?\n";
            }
        }
    }

I.h

enum class status { Edge, Present, Transcendent };
enum Handed { Left = 1, Right };
const int Longest_Range = 1;
const int Max_Energy = 2;

输出到脚本文件

Here???? this = 00059610 with energy = 2
X1121P
X1221P
X2121P
X2221E

0 个答案:

没有答案