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.
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
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.