How to increase the maximum memory allocated on the stack/heap

  • How can one increase the maximum memory allocated on the stack/heap for a program in C++?

  • Will increasing the RAM of your computer automatically increase the stack/heap memory of a computer program?


  • In Visual C++ you may use directive #pragma . For example:

    #pragma comment(linker, "/STACK:2000000")
    #pragma comment(linker, "/HEAP:2000000")
    

    You can specify the reserved heap size and stack size with the link options /Heap and /Stack in Visual Studio. For details, check these MSDN articles:

  • Heap Allocation
  • Stack Allocation

  • Second edit: I see from your comment that you work in Windows, so my Unix answer below would not be very helpful to you. But see Determining Stack Space with Visual Studio and C/C++ maximum stack size of program.

    The stack size is quite often limited in Linux. The command ulimit -s will give the current value, in Kbytes. You can change the default in (usually) the file /etc/security/limits.conf.

    You can also, depending on privileges, change it on a per-process basis using setrlimit() . See for example my answer to Segmentation fault: Stack allocation in a C program in Ubuntu when bufffer>4M.

    For heap , see eg Heap size limitation in C. But I don't believe you can increase or decrease the maximum size.

    链接地址: http://www.djcxy.com/p/78702.html

    上一篇: 静态类变量在内存中存储在哪里?

    下一篇: 如何增加堆栈/堆上分配的最大内存