crash when i have big input (C++)

Why the program crashes when i have f.e n=1.000.000?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream fout;
    ifstream fin;
    fin.open("share.in");
    fout.open("share.out");

    signed int n;

    fin>>n;

    signed int array1[n];
        for(int i=0; i<n; i++)
    {
        fin >> array1[i];
    }


    signed int suma=0, sumb=0, sumc=0, a=1, b=1, c=n-2, a1=0, b1=0, c1=0, mikroterosum=1000000000;

        while(c>0)
        {
                while(a1<a)
                {
                suma+=array1[a1];
                a1++;
                }
                while(b1<b)
                {
                sumb+=array1[(a1+b1)];
                b1++;
                }
                while(c1<c)
                {
                sumc+=array1[(a1+b1+c1)];
                c1++;
                }



        if(max(suma,max(sumb,sumc))<=mikroterosum)
            {
                mikroterosum=min(mikroterosum, max(suma,max(sumb,sumc)));
            }



                suma=0;
                sumb=0;
                sumc=0;

                a1=0;
                b1=0;
                c1=0;

                c--;
                b++;


    if(c==0)
        {
            ++a;
            b=1;
            c=n-a-1;
        }
            }


    fout<<mikroterosum;


    fin.close();
    fout.close();

    return 0;
}
First of all, it is not standard C++. Array dimesions should be known in compile time.
I assume you are using GCC VLA extension here, in this case, array1 is allocated on stack. If you request it to be of size 1.000.000, it will take slightly less (assuming 4 byte int) than 4 megabytes of memory, which is larger than common stack size.
so what i have to do?
Do not use stack allocated arrays for big data.

Use C++ features. Like vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int n; //signed is redundand
fin >> n;
std::vector<int> array1(n);
for(int i=0; i<n; i++)
    fin >> array1[i];
//Alternative
int n;
fin >> n;
std::vector<int> array1;
array1.reserve(n);
std::istream_iterator<int> input(fin);
for(int i=0; i<n; i++)
    array1.push_back(*input++);
//If there is nothing in file aside from those n numbers:
int n; 
fin >> n;
std::vector<int> array1(std::istream_iterator<int>(fin), std::istream_iterator<int>());
neither way works. Terminal does nothing.
neither way works
It does. Are you sure that file is opened properly? Are you sure that following code is not bugged?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::ifstream fin("share.in");
    if(!fin.is_open()) {
        std::cerr << "Failure opening file\n";
        std::exit(EXIT_FAILURE);
    }
    std::size_t n;
    fin >> n;
    std::vector<int> array;
    std::copy_n(std::istream_iterator<int>(fin), n, std::back_inserter(array));
    if(array.size() != n || fin.fail()) {
        std::cerr << "failure to read all values\n";
        std::exit(EXIT_FAILURE);
    }
    for(auto i: array)
        std::cout << i << ' ';
}
What is the output?
Either turn on C++11 (which you should do as most people use it extensively) or replace relevent parts with C++98 equivalent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <fstream>
#include <iostream>
#include <vector>

int main()
{
    std::ifstream fin("share.in");
    if(!fin.is_open()) {
        std::cerr << "Failure opening file\n";
        std::exit(EXIT_FAILURE);
    }
    std::size_t n;
    fin >> n;
    std::vector<int> array;
    for(std::size_t i = 0; i < n; ++i) {
        int x; fin >> x;
        array.push_back(x);
    }
    if(array.size() != n || fin.fail()) {
        std::cerr << "failure to read all values\n";
        std::exit(EXIT_FAILURE);
    }
    for(std::size_t i = 0; i < n; ++i)
        std::cout << array[i] << ' ';
}

Topic archived. No new replies allowed.