C++ 在阿托尔之后随机崩溃

C++ Random crash after on atol

本文关键字:之后 随机 崩溃 C++      更新时间:2023-10-16

我不知道为什么我的程序在int iNiv = atol(LES_EMPLOYES[i][j + 2])上崩溃;当i = 9时,其他一切都完美无缺。这让我发疯。

int j;
for (int i = 0; i <= NB_EMPLOYES - 1; i++)
{
    j = 0;
    string sNom = LES_EMPLOYES[i][j];
    j += 1;
    int iNum = atol(LES_EMPLOYES[i][j + 1]);
    j += 1;
    int iNiv = atol(LES_EMPLOYES[i][j + 2]);
    CEmploye* unEmploye = new CEmploye(sNom, iNum, iNiv);
    tabEmployes[i] = unEmploye;
}
const char* LES_EMPLOYES [NB_EMPLOYES] [3] =
{
    { "Kashmir Ducom",      "7301",  "1"},
    { "Zanael Batard",      "7302",  "1"},
    { "Azilis Tapin",       "7303",  "2"},
    { "Mayeul Malfait",     "7304",  "2"},
    { "Alexiam Castorix",   "7305",  "3"},
    { "Zoemy Malapry",      "7306",  "3"},
    { "Capri Lagarce",      "7307",  "1"},
    { "Samsara Gaudiche",   "7308",  "4"},
    { "Ghessy Grommolard",  "7309",  "3"},
    { "Abyalex Fayot",      "7310",  "5"}
};

当您尝试访问第三个元素时,您超出了界限,这会导致未定义的行为:

j = 0;
string sNom = LES_EMPLOYES[i][j]; // Access index 0 first element: OK
j += 1;
int iNum = atol(LES_EMPLOYES[i][j + 1]); // Access index 2, THIRD element: OK? My guess is that you would like to access the second element (index 1)
j += 1;
int iNiv = atol(LES_EMPLOYES[i][j + 2]); // Access index 4, Out of bound problem: Undefined Behavior

您应该删除您的线路j += 1;