0

I'm am doing a program (machine learning course) in which I have to read 120.000 files, and save each line of each file in a string[]. If a line repeats I just do not add it in my dictionary. I have declared a string dic[100000] and with it I'am able to handle more or less 20.000 files, not more. The problem is that if I declare string dic[200000] program compiles but crashes in .exe, same if I declare dic[100000], dic2[100000]. Can anyone suggest me any method to use memory in a proper manner so that I can handle 120.000 files?

1 Answers1

-2

You are probably exceeding the stack size of your program. That's something that can be configured when compiling as far as I can remember; in any case, you should be using the heap instead of the stack; this is: use dynamic memory. Example:

string *dic = new string[200000];
...
delete [] dic;

As you can see, you have to take care of releasing the memory in this case, but you can use much more memory.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • Ok I'm going to try. – Luigi Bernardi Nov 08 '17 at 11:32
  • 5
    This is considered bad practice. If your really-really want to stick to `string[]` then do `std::unique_ptr dic(new string[20000])` but you are much better off using `vector` as others already suggested. – Botond Dénes Nov 08 '17 at 11:32
  • 2
    Or even better, use `std::deque` to avoid huge contiguous allocations. – Botond Dénes Nov 08 '17 at 11:33
  • It seems to work!!! How can I transform to dynamic this matrix? float MB [111000] [2]; – Luigi Bernardi Nov 08 '17 at 11:34
  • Botond Denes, sorry I'm not a pro c++ user. Just a beginner. In order to use std::deque<...> I have just to add it at the top of my program or something else? And to use vector do I have to change the syntax all over the program? – Luigi Bernardi Nov 08 '17 at 11:36
  • You just need to include the appropriate header (`#include ` or `#include `) and of course the declaration of your `dic` variable. Other than that you can use it the same way. – Botond Dénes Nov 08 '17 at 11:41
  • If I try to do std::unique_ptr dic(new string[650000]); it gaves me these errors: unique:ptr is not a member of 'std', expected primary-ex befor [, dic was not declared in this scope – Luigi Bernardi Nov 08 '17 at 11:51
  • Actually the point of my answer was to explain why he was getting that error. The example is a pretty simple one. Apart from that, I don't think that's a bad practice *by default*. If he were implementing a class, depending on the needs it could make sense to allocate the array on instantiation time and deallocating it in the destructor. Which doesn't mean `std::unique_ptr` isn't a good option, of course. In my opinion using `vector` should be restricted depending on the needs. If the size of the array is fixed, using such class only introduces some unnecessary overhead. – Valerio Sevilla Nov 08 '17 at 12:20