is there some way to enlarge the heapsize of a c++ program? In android you can easily do that by declaring it as large in the manifestfile.
I encountered this problem when I tried to allocate one billion array-elements on the heap.
int main() {
int size = 1000000000;
int* nmbr = new int[size];
return 0;
}
C++ itself doesn't know what the "heap" is. This is an implementation issue, so you have to look into your compiler's documentation. Here's what a Google search for "MSVC heap size flag" found:
http://msdn.microsoft.com/en-us/library/f90ybzkh.aspx
Quoting from the MSDN page:
The /HEAP option sets the size of the heap in bytes. This option is only for use when building an .exe file.
Usually the heap size is implementation specific and depends on the compiler you are using. Have a look at the documentation, most provide a way to change the heap size.
To give you a more general answer:
The heap can be any size, you can't really compare it to your computers memory. Just because your computer might have 4GB of memory doesn't mean that every program can use as much. The default heap size in MS Visual C++ is 1MB but your program will still use up much more than 1MB while running. The memory your program consumes also consists of stack memory, handles and threads used by the operating system and last but not least the program code and all its contained strings and other literals.
The main problem in allocating memory is how to address it. This is a major difference between x86 and x64 architectures. On a 32-bit windows operating system, your program will typically only have 2GB of virtual memory available (or 3GB with special settings).
You will notice someday that you can get Out of memory-errors even when there is plenty of space left. This can happen if the runtime memory is so fragmented that the runtime cannot fit the requested amount in between anywhere and usually is caused by frequent memory leaks. To avoid this, applications which need large amounts of memory will try to allocate it in a big chunk before doing any real work and will have some sort of memory management to handle the allocations themself (SQL Databases might do this). This way they are able to rearrange the data inside the memory according to their needs. There are various methods to do this, in Windows you might use VirtualAlloc
or some other form of allocation.
来源:https://stackoverflow.com/questions/22794439/enlarge-heapsize-c