How to prevent stack overflow when using array in C++?

-2

I'm writing a program in C++ that calls a function in each iteration of a for-loop and stores the "results" in a .csv file. The function takes in 18 parameters and stores the results in an array. When I run the program with 10,000 iterations it works fine.
However, when I increase the iteration number to 100,000, it shows the stack overflow error:

"Unhandled exception at 0x00007FF6E76D8408 in ConsoleApplication.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000ABB1693000)."

The name of the function is wes5.cpp which is a void function. It stores the calculated results in the "results" array. Note that some of the inputs of the function are randomly generated using for- loop. Can anyone tell me why it works fine with 10000 iterations and does not work with 100,000?

#include <iostream>
#include "wes.hpp"
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
#include <fstream>
int main()
{
    srand(time(NULL));

    const int num_E = 100000;  // number of itteration

    double E1_max = 2000.0 * 1000; double E1_min = 300.0 * 1000;
    double E2_max = 100.0 * 1000; double E2_min = 1.0 * 1000;
    double E3_max = 100.0 * 1000; double E3_min = 1.0 * 1000;

    int num_H = 100.0;
    double H1_max = 12; double H1_min = 3;
    double H2_max = 25; double H2_min = 17;

    int num_L6000 = 100000; 
    double L6000_max = 7000; double L6000_min = 5000;

    //matrix declration for E
    double E1[num_E]; double E2[num_E]; double E3[num_E];
    double E4[num_E];double E5[num_E];

    //matrix declration for H
    double H1[num_E]; double H2[num_E]; double H3[num_E];
    double H4[num_E];double H5[num_E];

    //matrix declration for q
    double q[num_E]; 

    // variable description
    double u1 = 0.35; double u2 = 0.45; double u3 = 0.45; double u4 = 0.45; double u5 = 0.45;
    double la1 = 1; double la2 = 1; double la3 = 1;
    int l = 1;
    double p[] = { 0 };
    double a[] = { 5.9 };
    double xc[] = { 0 };
    double yc[] = { 0 };
    int l1 = 1;
    int ls = 1;
    double xs[] = { 0 };
    double ys[9] = {};
    double zs[] = { 0 };
    double results[900];
    //double my_results[10]; //num_E
    double X[9] = { 0, 8, 12, 18, 24, 36, 48, 60, 72 };

    for (int i = 0; i < num_E; i++)     //  Random data generation for E
    {
        int num_items_E1 = E1_max - E1_min + 1;
        int num_items_E2 = E2_max - E2_min + 1;
        int num_items_E3 = E3_max - E3_min + 1;

        E1[i] = E1_min + rand() % num_items_E1;
        E2[i] = E2_min + rand() % num_items_E2;
        E3[i] = E3_min + rand() % num_items_E3;
        E4[i] = E3[i];
        E5[i] = E3[i];
    }


    for (int i = 0; i < num_E; i++)     // Random data generation for H
    {
        int num_items_H1 = H1_max - H1_min + 1;
        int num_items_H2 = H2_max - H2_min + 1;

        H1[i] = H1_min + rand() % num_items_H1;
        H2[i] = H2_min + rand() % num_items_H2;
        H3[i] = 999;
        H4[i] = 999;
        H5[i] = 999;
    }


    for (int i = 0; i < num_E; i++)     // Random data generation for q
    {
        int num_items_load = L6000_max - L6000_min + 1;
        double load = L6000_min + rand() % num_items_load;
        q[i] = load / (3.141652 * pow(a[0], 2));
    }

    std::fstream my_file;
    my_file.open("Output.csv");
    my_file << "E1" << "," << "E2" << "," << "E3" << "," <<
        "H1" << "," << "H2" << "," << "H3" << "," <<
        "q" << "," << "D0" << "," << "D1" << "," << "D2" << "," << "D3" << "," << "D4" << "," <<
        "D5"<< "," << "D6" << "," << "D7" << "," << "D8" << "\n";

    // running the main program
    for (int i = 0; i < num_E; i++) {

        double e1 = E1[i]; double e2 = E2[i]; double e3 = E3[i]; double e4 = E4[i]; double e5 = E5[i];
        double h1 = H1[i]; double h2 = H2[i]; double h3 = H3[i]; double h4 = H4[i]; double h5 = H5[i];
        p[0] = q[i];

        for (int j = 0; j < 9; j++) {

            ys[0] = X[j];

            wes5(&e1, &e2, &e3, &e4, &e5,
                &h1, &h2, &h3, &h4, &u1, &u2, &u3, &u4, &u5,
                &la1, &la2, &la3, &l,
                p, a, xc, yc,
                &l1, &ls, xs, ys, zs,
                results);

            //my_results[i] = results[700];

            if (j == 0) {
                my_file << E1[i] << "," << E2[i] << "," << E3[i] << "," <<
                    H1[i] << "," << H2[i] << "," << H3[i] << "," <<
                    p[0] << "," << results[700] << ",";
            }
            else {
                my_file << results[700] << ",";
            }


        }

        my_file << "\n";

    }
    my_file.close();

}
c++
arrays
for-loop
memory
stack-overflow
asked on Stack Overflow Jan 3, 2020 by Mostafa Nakhaei • edited Jan 3, 2020 by Mostafa Nakhaei

1 Answer

2

Judging by your error, you are using Visual Studio, which has a default stack size of 1MB. In your code, you allocate 10 arrays of 100,000 doubles. Each of these arrays, therefore, is 800,000 bytes (double is 8-byte). Multiply that by 10, and you are up to 8MB of stack size for just your arrays. This is why you are getting a stack overflow error.

With size 10,000 you use 0.8MB of stack for your arrays, which is small enough to be able to still work.

You can use the /F option to change the stack size, but I'd recommend rethinking your code instead. Perhaps a vector would be better?

answered on Stack Overflow Jan 3, 2020 by ChrisMM

User contributions licensed under CC BY-SA 3.0