stackoverflow C ++中的异常在while循环

时间:2018-03-17 18:53:13

标签: c++ sql-server exception odbc

我一直在寻找有关这方面的错误但无法找到它的洞察力;我可能会忽略一些愚蠢的事情。我希望你能看到它!

我在C ++ pgm中有一个while循环,我使用ODBC将SQL Server数据加载到3个不同的数组中(每列1个)。如果我在while循环之外增加数组计数器(在代码上面,注释掉),当然它不会推进数组条目,但它不会爆炸。如果我将增量​​(rowNum ++)移动到while循环中(如图所示),我会得到一个stackoverflow异常。请问有人给我一个提示吗?非常感谢!

    else {
        short iptMKTNUM;
        short iptDAYNUM;
        float iptPX;
        int rowNum = 0;
        //rowNum++;
        while (SQLFetch(SQLStatementHandle) == SQL_SUCCESS) {
            SQLGetData(SQLStatementHandle, 1, SQL_C_DEFAULT, &iptMKTNUM, sizeof(iptMKTNUM), NULL);
            SQLGetData(SQLStatementHandle, 2, SQL_C_DEFAULT, &iptDAYNUM, sizeof(iptDAYNUM), NULL);
            SQLGetData(SQLStatementHandle, 3, SQL_C_FLOAT,   &iptPX,     sizeof(iptPX),     NULL);
            MktNum[rowNum] = iptMKTNUM;
            DayNum[rowNum] = iptDAYNUM;
            Price[rowNum] = iptPX;
            cout << "Mkt/Day/Px IS " << iptMKTNUM << " " << iptDAYNUM << " " << iptPX << endl;
            cout << "Mkt/Day/Px IS " << MktNum[rowNum] << " " << DayNum[rowNum] << " " << Price[rowNum] << endl;
            cout << "rowNum is " << rowNum << endl;
            rowNum++;
        }
    }

1 个答案:

答案 0 :(得分:1)

您是如何分配数组MktNumDayNumPrice的?这个重要的部分是你所要求的,因为你不允许在他们的范围之外访问阵列。

可能您应该使用vectors而不是数组,然后使用push_back附加值。你可以从向量的大小得到的行数。

 vector<short> MktNum;
 vector<short> DayNum;
 vector<float> Price;
 while (SQLFetch(SQLStatementHandle) == SQL_SUCCESS) {
        SQLGetData(SQLStatementHandle, 1, SQL_C_DEFAULT, &iptMKTNUM, sizeof(iptMKTNUM), NULL);
        SQLGetData(SQLStatementHandle, 2, SQL_C_DEFAULT, &iptDAYNUM, sizeof(iptDAYNUM), NULL);
        SQLGetData(SQLStatementHandle, 3, SQL_C_FLOAT,   &iptPX,     sizeof(iptPX),     NULL);
        MktNum.push_back(iptMKTNUM);
        DayNum.push_back(iptDAYNUM);
        Price.push_back(iptPX);
        cout << "Mkt/Day/Px IS " << iptMKTNUM << " " << iptDAYNUM << " " << iptPX << endl;
        cout << "Mkt/Day/Px IS " << MktNum.back() << " " << DayNum.back() << " " << Price.back() << endl;
        cout << "rowNum is " << (-1 + MktNum.size()) << endl;
    }