why application has requested the Runtime to terminate it in an unusual way?

Hello everyone I am trying to read from a file and save every line of the file into a array. The file's total size is uknown so I am dynamically allocating the memory. If I read data from a small file (<1 mb) everything is good but when I try to run the program and use a 70mb file it gives me this


This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.



and the program close immediately!!!

Here's my code:

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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
  fstream f;
  string *b = new string[1];
  string *tmp;
  int size=1;
  f.open("Books.txt");
  while (!f.eof())
  {
    string a;
    getline(f,a);
    tmp= new string[size+1];
    copy (b,b+size, tmp );
    free(b);
    b= new string[size];
    tmp[size-1]=a;
    b=tmp;
    free(tmp);
    size++;	
    cout<<size<<"\n";
 }
 f.close();
 system("PAUSE");
 return 0;
}


any ideas what's is going on?
Last edited on
http://www.cplusplus.com/reference/vector/vector/
1
2
3
4
std::vector<std::string> document;
std::string line;
while(std::getline(f,line))
   document.push_back(line);


About your error, you are mixing `free()' with `new'. Don't do that.
Allocate with `new', deallocate with `delete'
Allocate with `new[]', deallocate with `delete[]'
thanks for the advice, nevertheless the problem didn't solve, now the program reads more lines of the file but after a while it shows the same message and it closes. I don't know if it usefull but I have to mention that in both cases before and after the changes above always the ram usage reaches the 4,37 gb of totaly 8 gb ram and I'm thinking if I reach a limit and I can't use more ram for this program
Last edited on
Please post your updated code after incorporating ne555's suggestions.

It would be a whole lot cleaner to use a vector for what you want to do.
updated code :
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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
  fstream f;
  string *b = new string[1];
  string *tmp;
  int size=1;
  f.open("Books.txt");
  while (!f.eof())
  {
    string a;
    getline(f,a);
    tmp= new string[size+1];
    copy (b,b+size, tmp );
    delete(b);
    b= new string[size];
    tmp[size-1]=a;
    b=tmp;
    delete(tmp);
    size++;	
    cout<<size<<"\n";
 }
 f.close();
 system("PAUSE");
 return 0;
}


I know that using vector is easier but all these doing for a college's project and I must use array necessarily in this project
Line 21 should be delete[] b; (similar for line 25)

Also, don't loop on `eof()', it stops too late
Last edited on
Your code maximizes memory fragmentation. Every time you invoke new, a piece of contiguous memory must be found that is large enough to contain the size of memory you're asking for. You ask for a new piece of memory every iteration of your loop. Your strings are also requesting their own pieces of memory from new, further complicating things. So when you get to large memory sizes, new is likely to fail (be unable to find a large enough contiguous piece of memory.) Have you actually used try/catch to ensure that new isn't failing?

Do you really need to read in the whole file at one time? You can't process a portion at a time?
I must create a array that contains all elements of the file and after search on it with different ways(linear search,binary search, etc) so unfortunately I need read the whole file :( , I don't know if I open the file, read a part of time. close it and after repeat this process until read the whole file can finally give me a sollution :/
Within the loop, the new operator is used twice. One is to allocate a larger array. The other is not required. In the current code the second new gives a memory leak, as memory is allocated, then the value of the pointer is lost.
Chervil wrote:
Within the loop, the new operator is used twice.

Nice catch. Code with no white space makes my eyes hurt.
Topic archived. No new replies allowed.