Reputation: 7782
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?
Upvotes: 22
Views: 62343
Reputation: 1513
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.
Heap Reserve Size
and Stack Reserve Size
.Upvotes: 2
Reputation: 3177
In Visual C++ you may use directive #pragma
. For example:
#pragma comment(linker, "/STACK:2000000")
#pragma comment(linker, "/HEAP:2000000")
Upvotes: 9
Reputation: 2862
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.
Upvotes: 3
Reputation: 9972
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.
Upvotes: 7
Reputation: 6050
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:
Upvotes: 5