My c program prints "out of memory" -error, but a lot of free memory space is available

My program returns a memory error without using even more than 1 MB. I am writing C in the dev-cpp compiler. The original program is too large to fit here. This very simple program works for me:

int main(){int a[520076]; return 0;}

and returns a value of 0. However, this:

int main(){int a[520077]; return 0;}

The reason also does not work in memory. I use Windows 8, but the same problem occurs in Windows 7. It seems that the system limits the amount of memory that the process can use. Let the border also be set to dev-cpp?

+4
source share
1 answer

It looks like the system allocates maximum memory space

Yes, but this is not really a problem in this case. The array you declare is local and automatic, so it will most likely be allocated on the stack. The stack size is limited, even more limited than dynamically allocated memory (usually a lot) and with a small megabyte of large size (if I remember correctly, this is actually 1 MB on Windows by default), it quickly collides with other memory areas.

If you do not want this, declare your variable as a variable file-scope ("global") or as static (read about side effects).

In addition, you are right that the OS also limits all memory - it tries to maintain democracy between processes. Even if you run the program on a server with 128 gigabytes of RAM, malloc() will most likely fail, for example, if you want to allocate 32 gigabytes. Such memory is not needed at all, and the OS protects other processes from a single erroneous (or intentional) supply of resources.

+5
source

All Articles