2

In many dynamic programming and graph problems it is required to do long depth recursion.

I am currently using vscode and mingw in windows for my c++ programs. But in default,as per my knowledge windows has 1MB max stack size .So I gets segmentation fault / stackoverflow problems. I know pretty well that I can change every recursion in loop,but i don't wanna that stuff.

In some programming contest like Google Hashcode,Facebook Hackercup , they give large input and if I run that input in my machine it faces segmentation fault / stackoverflow problems.

Now what i need is to increase max stack size.

I found some approaches and here is my questions.

  1. g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 Untitled1.cpp -o a.exe It works pretty well when I use this command in windows command prompt. But it gives error in vscode terminal(i don't know why.) I found this command here. enter image description here

  2. I found somewhere #pragma comment(linker, "/STACK:2000000"), but i didn't understood this clearly.

  3. Is there anyway to change max stack size once in vscode, so that i do not need to specify every time when i compile ?

I just want to increase max stack size ,

JitendraJat
  • 53
  • 1
  • 8
  • 2
    That comma in `g++` command looks very suspicious. – Yksisarvinen Jul 28 '20 at 12:39
  • 2
    Do you need more size because you are using recursion, or do you need it because you have very large arrays? – NathanOliver Jul 28 '20 at 12:39
  • 2
    @Yksisarvinen the compiler even underlines the wrong comma – RoQuOTriX Jul 28 '20 at 12:40
  • @RoQuOTriX as i mentioned , i am not getting error in command prompt. – JitendraJat Jul 28 '20 at 12:52
  • @JitendraJat what happens if you remove it? – RoQuOTriX Jul 28 '20 at 12:56
  • @RoQuOTriX i got this after removing that one comma g++.exe: error: unrecognized command line option '-Wl'; did you mean '-W'? g++.exe: error: unrecognized command line option '--stack=268435456' – JitendraJat Jul 28 '20 at 13:01
  • Do you know what `-Wl` does? Why do you use it? There should be any link options behind `-Wl` – RoQuOTriX Jul 28 '20 at 13:04
  • 1
    If you have an algorithm that requires huge stack size, the first thing to do is to rethink that algorithm rather than increase stack size. Unless you want to use an algorithm that is fundamentally broken. – Evg Jul 28 '20 at 13:09
  • 1
    The comma error has to do with powershell attempting to parse the command: See https://stackoverflow.com/q/62072005/2721883 although that doesn't have a solution due to other issues, you can use `--%` to stop powershell from parsing the rest of the command (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7) – clcto Jul 28 '20 at 13:13
  • 1
    You can increase the stack as well as heap size in project properties. It would also be a good idea to simulate the working of a recursive DFS function using your own stack before the next round. – srt1104 Jul 28 '20 at 13:15
  • @Evg that's not always the case. – srt1104 Jul 28 '20 at 13:18

1 Answers1

3

VSCode uses powershell as its shell and commas are special characters for powershell. You need to put them inside quotes.

g++ -O2 -std=c++11 -Wall "-Wl,--stack=268435456" Untitled1.cpp -o a.exe

should work.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243