-2

I keep searching for insight into what's wrong with this but can't find it; it is probably something dumb that I'm overlooking. I hope you can see it!

I have a while loop in a C++ pgm where I am loading SQL Server data using ODBC into 3 different arrays (1 for each column). If I increment the array counter outside the while loop (above in the code, commented out), of course it doesn't advance the array entries, but it does not blow up. If I move the increment (rowNum++) into the while loop (as shown), I get a stackoverflow exception. Can anyone give me a hint please? Thanks much!

    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++;
        }
    }
Timbo1711
  • 7
  • 1
  • 3
    Post [mcve]. Your code snipped is not relevant to the issue. Probably you access out of array bounds. – 273K Mar 17 '18 at 19:00

1 Answers1

1

How did you allocate your arrays MktNum, DayNum, Price? This important part you held back, as you are not allowed to access the arrays out of their range.

Probably you should use vectors instead of arrays and then use push_back to append the values. The number of rows you can get from the size of the vectors.

 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;
    }
milbrandt
  • 1,438
  • 2
  • 15
  • 20
  • I finally got thru it. The problem was my array sizes. I was overflowing them. They are about 10.5 million entries each. I found a message that said to either define the arrays as "in file scope", or simply preface with the "static" parm. I did the latter and it solved it. The load is fast, maybe 10 seconds to fill from SQL Server. Do you think I should try the vector approach, or just move on? THANKS for your advice by the way! – Timbo1711 Mar 17 '18 at 21:02
  • Depends on your use case. `vector`s are the preferred way.https://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap You should try to avoid handling memory by yourself with new and delete when there is a solution which takes care of it.. – milbrandt Mar 17 '18 at 21:06