midul's blog

By midul, 13 years ago, In English

in many code i see that type of "something". anybody explain please.

  • Vote: I like it
  • +8
  • Vote: I do not like it

»
13 years ago, # |
  Vote: I like it +3 Vote: I do not like it

It sets stack size. If you don't write it, your solution may crash with stack overflow (in deep recursive functions, for example).

  • »
    »
    13 years ago, # ^ |
      Vote: I like it +15 Vote: I do not like it

    The only thing to add is that it's MS Visual C++-specific and won't work with GCC. In GCC you should specify -Wl,--stack=256000000 command line option to set stack size (in bytes).

    • »
      »
      »
      13 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      thats nice. by the by whats the default gcc stack size?

      • »
        »
        »
        »
        13 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        2MB. You can see this in the headers of .exe produced by MinGW.

    • »
      »
      »
      13 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it
      $ g++ -Wl,--stack=256000000 test.cpp
      /usr/bin/ld: unrecognized option '--stack=256000000'
      /usr/bin/ld: use the --help option for usage information
      

      Parameter --stack can be used only with MinGW. ;)

      • »
        »
        »
        »
        13 years ago, # ^ |
          Vote: I like it -13 Vote: I do not like it

        so how increase stack in g++ under CNU/Linux?

        • »
          »
          »
          »
          »
          13 years ago, # ^ |
            Vote: I like it +13 Vote: I do not like it

          Nohow. Stack size is a user limit under GNU/Linux and doesn't depend on compiler. You can use ulimit -s to change/view it.

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      can you tell me exactly what should I write in the code for GCC compiler to increase the stack size?

      • »
        »
        »
        »
        10 years ago, # ^ |
        Rev. 4   Vote: I like it +21 Vote: I do not like it

        If inline assembler is allowed, you can allocate a block of memory using malloc and move the stack pointer to that newly allocated block. AFAIK, it's the only way of increasing the stack size from the code in GCC. Sorry, but I don't know AT&T syntax, which is used by GNU Assembler, so I can't write the code.

        But in MSVC (which uses Intel syntax) it'd look like this:

        void main2()
        {
        	// Do work
        }
        
        int main()
        {
        	size_t stack_size = 1 << 26;
        	char *newstack = (char*)malloc(stack_size);
        	char *esp_pos = newstack + stack_size - sizeof(int);
        	__asm {
        		mov eax, esp_pos
        		mov [eax], esp
        		mov esp, esp_pos
        	}
        	main2();
        	__asm {
        		pop esp
        	}
        	free(newstack);
        }
        

        UPD. Fixed an error.

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it +10 Vote: I do not like it

        You are not able to modify stack size from source code using GCC, afaik (however, I've heard that there were some extensions in the latest versions of GCC, but I don't know exactly). The only way to do so was described above. For example, in Windows you can use the following command line: g++ -o app.exe -Wl,--stack=256000000 -O2 source.cpp

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it +5 Vote: I do not like it

        Thanks both of you very much.

»
13 years ago, # |
  Vote: I like it +8 Vote: I do not like it

“#pragma comment(linker, ”/STACK:36777216“)” Is it usable only in the on-line judge or anywhere you want?

  • »
    »
    13 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    It's usable anywhere with Microsoft Visual C++ — on your local machine and so on.