-1

I m using minGW compiler along with VScode on windows 10

Problem I m facing is provided below :

here I can see output while using an arr of vectors of size 1500

Hello World
Execution works

enter image description here

.

.

.

But on declaring an arr of vectors of large size I cannot receive any output in terminal enter image description here

How will I be able o work with an arr of vectors of size 150000 or more ?

rioV8
  • 24,506
  • 3
  • 32
  • 49
humbleCodes
  • 109
  • 1
  • 6
  • 1
    You're running out of stack with big arrays ([see this](https://stackoverflow.com/questions/1825964/c-c-maximum-stack-size-of-program)). – Shahriar Jan 21 '22 at 06:44
  • 2
    @AnoopRana the OP intentionally wants array of vectors. – Shahriar Jan 21 '22 at 06:46
  • You can create dynamically on heap instead of stack with new operator and/or any smart pointers. – Karol T. Jan 21 '22 at 06:47
  • 1
    [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) reads: *DO NOT post images of code, data, error messages, etc. - copy or type the text into the question.* – Evg Jan 21 '22 at 07:12
  • @Evg I will definitely take care of this in future. – humbleCodes Jan 21 '22 at 07:16
  • this is question N where they want 150000 vectors of vectors, what is the problem they want to solve – rioV8 Jan 21 '22 at 10:50

1 Answers1

0

You blew up the stack. Every std::vector object occupies 24 bytes on the stack (in GCC's implementation). Now 150000 * 24 = 3.6 MB which may easily cause a problem for you.

Instead, try this (one of the easiest ways):

std::vector< std::vector<int> > v( 150'000 );

or maybe this:

std::vector< std::vector<int> > v( 150'000, std::vector<int>( 1000 ) );

The above statement will create a vector that owns 150000 other vectors each of which has 1000 elements initialized to 0.

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • Or just `v( 150'000 );` – Evg Jan 21 '22 at 07:26
  • @Evg Right. Added that. – digito_evo Jan 21 '22 at 07:30
  • @digito_evo thnx for helping, btw in using VScode + minGW compiler, so by editing what settings in VScode can I have the stack size increase upto 5 MB ? – humbleCodes Jan 21 '22 at 07:55
  • @user14106566 There are a few factors that determine the stack size of a program. It can not be done just by editing settings in VS Code (VS code is just an editor). Nonetheless, similar questions have been asked on this site several times. You can read their answers. You may also search on Google and find a good answer for that. – digito_evo Jan 21 '22 at 08:02