This may sound silly, but I'm a little confused right now. Why won't this program consume all the memory? For example: I have the following program running on a Linux terminal (2G RAM),
#include <iostream> #include <cmath> using namespace std; int main() { cout<<sizeof(int*)<<endl; for(int i=0; i<pow(2.0,30.0);i++) { new int(i); } return 1; }
1) I confirmed that the int size in this machine is 4 bytes, then for 2 GB of memory, it can only hold 2 ^ 30/2 ^ 2 = 2 ^ 28
2) following the above logic, how could you change the program, actually consume all the memory of 2 GB?
Added: I just want to make sure that I understand it correctly. If there is no virtual memory or os optimization, etc., 2GBRAM can only contain 2 ^ 28 int, right? In this case, will the above program consume all the memory? You know how I can disable virtual memory / swap functions, etc. In Linux?
Thanks!
source share