Блог пользователя midul

Автор midul, 12 лет назад, По-английски

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

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
12 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

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

  • »
    »
    12 лет назад, # ^ |
      Проголосовать: нравится +15 Проголосовать: не нравится

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

    • »
      »
      »
      12 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      12 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится
      $ 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. ;)

      • »
        »
        »
        »
        12 лет назад, # ^ |
          Проголосовать: нравится -13 Проголосовать: не нравится

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

        • »
          »
          »
          »
          »
          12 лет назад, # ^ |
            Проголосовать: нравится +13 Проголосовать: не нравится

          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 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

      • »
        »
        »
        »
        10 лет назад, # ^ |
        Rev. 4   Проголосовать: нравится +21 Проголосовать: не нравится

        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 лет назад, # ^ |
          Проголосовать: нравится +10 Проголосовать: не нравится

        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 лет назад, # ^ |
          Проголосовать: нравится +5 Проголосовать: не нравится

        Thanks both of you very much.

»
12 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

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

  • »
    »
    12 лет назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

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