Problem with reading big amount of numbers from file Pin

Hi! I have to do a project for my studies. It is a insertion algorithm.
I'm reading data from file with using vector.
then I'm copying vector to a normal array (the reason is simple: I don't know how to send vector to function, but this is not a main problem).
After copying, I'm sending (with using pointers) array to sorting function.
Everything is good, algorithm is working but! (yep, there is always but... Wink
When I save a lot of data to file, program just after start crashes.

The numbers in file are written in that way:

1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

So there are 40 numbers in one line. I have 4.000 lines, so after simple math we have 160.000 numbers. The main problem: when I add some new numbers for example 40.000 program crashes I must have more numbers because the task is to compare few sorting algorithms with minimum 15 minutes of sorting. (160.000 digits is sorting in ~1 minute)

Here's the code. Maybe it is poor written, but I did my best.

//written with Qt
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
82
83
84
85
86
87
88
89
90
91
92
#include <QtCore/QCoreApplication>
#include <iostream>
#include <time.h>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

//sorting function
void insertion_sort(int *wsk, unsigned long size);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //vector to hold data from file
    vector <int> array_vector;

    //stream to file
    ifstream plik ("numbers.txt",ios::in);

    //single number that is being readed from file    
    unsigned long single_number;

    
    while(plik >> single_number)
    {
        array_vector.push_back(single_number);
    }

    int array_nonsorted[array_vector.size()];
    //copying from vector to array
    for (unsigned long a = 0; a < array_vector.size(); a++)
    {
        array_nonsorted[a] = array_vector[a];
    }

    time_t start,end; double long dif;

    cout << "START\n";
    time(&start);
    insertion_sort(array_nonsorted,array_vector.size());
    time(&end);
    cout << "STOP\n";
    dif = difftime(end,start);
    cout << "Array sorted in " << setprecision(5) << dif << "sec"; dif = dif/60; cout << " = " << dif << "minutes";
    //cout << "End.";
    return a.exec();
}
void insertion_sort(int *wsk, unsigned long size)
{
    int tab_sorted[size];

    //In final version I will do this by using pointers. I'm just copying it because with using pointers it's hard to see data in debugger
    for(unsigned long i = 0; i < size; i++)
    {
        tab_sorted[i] = wsk[i];
    }

    //main sort engine

    for (unsigned long a = 1; a < size; a++)
    {
        if (a == 1)
        {
            if (tab_sorted[0] > tab_sorted[1])
            {
                unsigned long temp = tab_sorted[0];
                tab_sorted[0] = tab_sorted[1];
                tab_sorted[1] = temp;
            }
        }
        else
        {
            for (unsigned long b = 0; b <= a-1; b++)
            {
                if (tab_sorted[a] < tab_sorted[b])
                {
                   unsigned long temp = tab_sorted[a];
                   for (unsigned long c = a; c > b; c--)
                   {
                       tab_sorted[c] = tab_sorted[c-1];
                   }
                   tab_sorted[b] = temp;
                }
            }
        }

    }
}


So why the program crashes? I don't have a clueFrown Something with memory (but I have 3.0GB and OS works normally when program is starting). Please help!
Luk.
Last edited on
Lines 33 and 54 are illegal in C++. Stack arrays have to be created with a constant size. Your compiler, probably GCC, is allowing it, but it's illegal.
The problem is that the array is so large that it overflows the stack.

I could tell you how to create an dynamically-sized array that can be very large, but instead I'm going to tell you how to pass a vector to a function and thus solve two problems at once.
1
2
3
4
5
6
7
std::vector<int> v;
function(v);
//...
void function(std::vector<int> &vector){
    for (size_t a=0;a<vector.size()-1;a++)
        vector[a]=vector[a+1];
}


I'm just copying it because with using pointers it's hard to see data in debugger
And it'll have to remain hard, because this cannot be solved with simple stack arrays. You have to use either pointers or vectors.
Thank you helios for helping me out! With sending vector it's working very well! Even with 1.000.000 numbers... :)

I know what is dynamically-sized array, I learned about it (the whole thing with creating array with
int *tabptr = new int[size] and using delete[] tabptr

I must admit I like this operators, but to be honest I had problems in this projects with dynamical array.
Problem was: everything is okay, when you know how many lines are in file. But when you don't, how to set proper size in array? I tried to count lines in while loop, but after that cursor was on the last byte of file, and when I was using tellg() and setg() functions to set it on first byte it still didn't worked.

So can you help me with this dynamical arrays? Is there trick to define array without knowing how many numbers are in file? Or should I use other functions than tellg() and setg()?
Last edited on
If you were to do this using dynamic arrays, you'd just end up implementing your own vector class, so there's no much point to it.
Topic archived. No new replies allowed.