22
  • 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?

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • 1
    I increase the ram , will it automatically increase my stack/heap size?? – Computernerd Dec 19 '12 at 02:56
  • usually each kernel impose a limit for thread and for process, you can use more resource going multi-process or multi-threading. – user1849534 Dec 19 '12 at 02:57
  • For the *stack*, you could try this answer: http://stackoverflow.com/a/13908277/318716 . – Joseph Quinsey Dec 19 '12 at 02:57
  • The stack? No. The stack is pre-allocated memory. For the heap, you will notice that you can allocate more dynamic memory (especially large blocks of them). – Mark Garcia Dec 19 '12 at 02:59
  • 2
    Meaningful answers will depend on the system you're targeting and (probably) the toolchain you're using. – Jerry Coffin Dec 19 '12 at 03:02
  • to be more specific: i am using the IDE:Visual Studio C++, Windows OS – Computernerd Dec 19 '12 at 03:03
  • Compiling your program in 64-bit mode will give it a much larger address space; 32-bit programs are typically limited to an address space of 4GB or less. 64-bit programs can address up to 2^64 bytes of RAM due to their larger pointer size. (Of course whether the hardware and/or OS actually allows the program to *use* that much RAM is another issue, but if you are running into heap size limitations, 32-bit pointers might be one of the causes) – Jeremy Friesner Dec 19 '12 at 03:51
  • 1
    Adding RAM improves the performance of a system, but does not usually affect the correct operation of a program. If your program needs more memory than you have RAM, Windows will move part of your program to disk in the swap file. – brian beuning Dec 19 '12 at 04:04

5 Answers5

9

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

#pragma comment(linker, "/STACK:2000000")
#pragma comment(linker, "/HEAP:2000000")
VeLKerr
  • 2,995
  • 3
  • 24
  • 47
  • Default stack and heap sizes are both 1 MB. See https://msdn.microsoft.com/en-us/library/8cxs58a6.aspx and https://msdn.microsoft.com/en-us/library/f90ybzkh.aspx – Andrew Jun 27 '16 at 12:52
7

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 e.g Heap size limitation in C. But I don't believe you can increase or decrease the maximum size.

Community
  • 1
  • 1
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
5

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:

  1. Heap Allocation
  2. Stack Allocation
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
Matt
  • 6,010
  • 25
  • 36
3

The heap size of a process is usually limited by the maximum memory the process can allocate. The heap does not need to be contiguous (unless you are doing something like malloc(1000000000)) so the heap can use most of the available address space.

Under Windows the maximum process size varies by a couple of factors.

Using 32-bit Windows, a 32-bit process can by default allocate 2 GB. If Windows is booted using the /3GB switch and the process is compiled using the "Enable Large Addresses" linker flag, then the process can allocate 3 GB.

Using 64-bit Windows, a 32-bit process by default can allocate 2 GB. If the process is linked with "Enable Large Addresses", then 64-bit Windows lets a 32-bit process allocate 4 GB.

A 64-bit process (on 64-bit Windows) can allocate something like 16,000 GB.

brian beuning
  • 2,836
  • 18
  • 22
2

I was getting some memory issue with default heap and stack reserve size of 1 MB. But when I set above properties to 2 MB (2000000), it works fine.

To set these properties in the Visual Studio development environment, please follow steps below.

  • Open the project's Property Pages dialog box.
  • Click the Linker folder.
  • Click the System property page.
  • Modify Heap Reserve Size and Stack Reserve Size.
the swine
  • 10,713
  • 7
  • 58
  • 100
Pabitra Dash
  • 1,461
  • 2
  • 21
  • 28
  • Did so and set it to 8 mb, say. The proj file reads 8000000 but the value does not appear when the project in VS10 reloads. Thanks, MS. – Laurie Stearn Jan 01 '17 at 11:11