How to get amount of RAM available on the PC

I bet there's a simple function that can return the amount of RAM available on the computer, or perhaps the total RAM on the chip.
Basically I want a way of making sure my program never tries to allocate memory when it can't... I know that Windows should stop this happening anyway but I want to make sure the program can take care of certain things if it happens.
Last edited on
I bet there's a simple function that can return the amount of RAM available on the computer, or perhaps the total RAM on the chip.


No. In any case, you can't hog all of your computers resources if you're running on an OS anyway, so the question is pointless for what you're trying to do. new throws and malloc returns NULL when it fails, just check for that. If you're dealing with large datasets, let the user decide how much memory you're allowed to use at once.
Cheers, I've never actually taken a look into handling exceptions though, perhaps it's time to take a look at the try and catch again?
agree with hanst99

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <windows.h>
#include <iostream>

using namespace std;

int main()
{
  MEMORYSTATUSEX status;
  status.dwLength = sizeof(status);
  GlobalMemoryStatusEx(&status);
  cout << status.ullTotalPhys;
  return 0;
}
Last edited on
Topic archived. No new replies allowed.