Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with reading big amount of numbers from file

Problem with reading big amount of numbers from file

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmstutorialiosgraphicsdata-structures
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vonpik
    wrote on last edited by
    #1

    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... ;) 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
    #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
    
    T D 2 Replies Last reply
    0
    • V vonpik

      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... ;) 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
      #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
      
      T Offline
      T Offline
      Tim Craig
      wrote on last edited by
      #2

      Have you built a debug version and used the debugger to see where it crashes? That's what I'd to as a starting point.

      You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

      V 1 Reply Last reply
      0
      • T Tim Craig

        Have you built a debug version and used the debugger to see where it crashes? That's what I'd to as a starting point.

        You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

        V Offline
        V Offline
        vonpik
        wrote on last edited by
        #3

        hmm, I didn't thought about this, because algorithm works fine, but I did it as you said, and just after debugger started error message came out Process stoped. Process was stopped because it received signal from operating system. Name of signal :SIGSEGV Meaning of singal : Segmentation fault Now it is coming to be more interesting..... ;) I can only click . After click nothing happened. Even debugging is still on, but do nothing. I read about SIGSEGV on wiki, but how to avoid it?

        T 1 Reply Last reply
        0
        • V vonpik

          hmm, I didn't thought about this, because algorithm works fine, but I did it as you said, and just after debugger started error message came out Process stoped. Process was stopped because it received signal from operating system. Name of signal :SIGSEGV Meaning of singal : Segmentation fault Now it is coming to be more interesting..... ;) I can only click . After click nothing happened. Even debugging is still on, but do nothing. I read about SIGSEGV on wiki, but how to avoid it?

          T Offline
          T Offline
          Tim Craig
          wrote on last edited by
          #4

          Sounds like you have a wild pointer somewhere. Or have managed to corrupt the heap.

          You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

          V 1 Reply Last reply
          0
          • T Tim Craig

            Sounds like you have a wild pointer somewhere. Or have managed to corrupt the heap.

            You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

            V Offline
            V Offline
            vonpik
            wrote on last edited by
            #5

            Maybe someone in the future will be searching solution for similar problem so I wrote it here. So the problem is with stack array. Array is so large that it overflows the stack. Solution is to use dynamically sized array. It can be vector class. In this example, it is sufficient to send pointer to function and in the sorting function working on vector. That's all. // Thanks to helios for helping me out (from other cplusplus forum)

            1 Reply Last reply
            0
            • V vonpik

              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... ;) 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
              #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
              
              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              vonpik wrote:

              int array_nonsorted[array_vector.size()];

              1.2MB in size, perhaps?

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              V 1 Reply Last reply
              0
              • D David Crow

                vonpik wrote:

                int array_nonsorted[array_vector.size()];

                1.2MB in size, perhaps?

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Man who follows car will be exhausted." - Confucius

                V Offline
                V Offline
                vonpik
                wrote on last edited by
                #7

                No, exact size doesn't matter. Just more than approximately 400.000 numbers.

                D 1 Reply Last reply
                0
                • V vonpik

                  No, exact size doesn't matter. Just more than approximately 400.000 numbers.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  You missed my subtlty. If you have 160,000 numbers and each is 4 bytes in size, that would require 1.28MB of stack space, obviously more than the default size of 1MB. Using the stack, I'd be surprised if you could get any more than 130,000 numbers.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Man who follows car will be exhausted." - Confucius

                  V 1 Reply Last reply
                  0
                  • D David Crow

                    You missed my subtlty. If you have 160,000 numbers and each is 4 bytes in size, that would require 1.28MB of stack space, obviously more than the default size of 1MB. Using the stack, I'd be surprised if you could get any more than 130,000 numbers.

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Man who follows car will be exhausted." - Confucius

                    V Offline
                    V Offline
                    vonpik
                    wrote on last edited by
                    #9

                    So, you are saying that default and maximum size of stack size is 1 MB? I solved this problem with using vector, but still I want to know a C++ mechanisms :)

                    D 1 Reply Last reply
                    0
                    • V vonpik

                      So, you are saying that default and maximum size of stack size is 1 MB? I solved this problem with using vector, but still I want to know a C++ mechanisms :)

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      vonpik wrote:

                      So, you are saying that default...size of stack size is 1 MB?

                      Yes.

                      vonpik wrote:

                      I solved this problem with using vector,

                      Which gets its memory from the heap, not the stack.

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Man who follows car will be exhausted." - Confucius

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups