C++ 在阿托尔之后随机崩溃
C++ Random crash after on atol
我不知道为什么我的程序在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;
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 在类定义之后定义一个私有方法
- 循环中的随机函数
- 在c++构造函数中使用随机字符串生成器
- 在循环C++中指定字符串之后,不会打印该字符串
- C++宏忽略之后的内容
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 使用std::mt19937从字符串中返回一个随机单词
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 如何在C++中高效地构造随机骰子
- 在类中使用随机生成器时出现性能问题
- 在将数字随机生成为数组期间从内存输出随机数的数组
- strncpy之后的char数组的错误行为
- 计算十进制 c++ 之后的数字
- 将字符随机转换为大写的函数
- "x += x--"之后的 x 是什么?
- C++ 在阿托尔之后随机崩溃