C ++在atol之后随机崩溃

时间:2015-04-19 21:38:45

标签: c++

我无法弄清楚为什么我的程序在int iNiv = atol(LES_EMPLOYES [i] [j + 2])上崩溃了;当我= 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"}
};

1 个答案:

答案 0 :(得分:0)

当您尝试访问第三个元素时,您将受到约束,这会导致Undefined Behavior

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;