简体   繁体   中英

C++ program to figure out how much memory my computer has?

Would the following work?

Initialize a pointer (to any type of object) to 0.

int* thisPtr = 0;

Perform pointer arithmetic by incrementing the pointer until it has reached the last memory location. We'll know it's the last memory location because adding 1 to the pointer will not do anything. Keep track of how many memory locations we've visited.

int count = 1;
 while (thisPtr + 1 > thisPtr) { 
    ++thisPtr;
    ++count;
}

Now count is equal to the number of memory locations we were able to visit. Multiply it by the number of bytes in a pointer.

int bytesInMemory = count * sizeof(int*);

Does that work??? If not, why, and what is the correct way?

No it doesn't.

In theory, what it does is undefined. In practice, it will always return 4GB for a 32-bit program, and take a ridiculous amount of time (as in: longer than the age of the universe) for a 64-bit program.

Pointers are just numbers. If you replace int* thisPtr with unsigned int thisPtr , replace thisPtr++ with thisPtr += 4 and replace thisPtr + 1 with thisPtr + 4 , you will get an equivalent program (on 32-bit systems). All it does is find the maximum possible value of an integer.

C++ does not have a way to find out how much memory your system has. The operating system will have a way to do it, but there isn't a standard C++ way that works on any operating system.

On modern computers memory has become a complex topic, though mostly you don't need to know very much.

To get the size of installed physical RAM, on Windows you do this:

    uint64_t nBytes=0;
    bool bOK = GetPhysicallyInstalledSystemMemory(nBytes);

See http://msdn.microsoft.com/en-us/library/windows/desktop/cc300158.aspx

On Linux, you might try:

    uint64_t nBytes = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);

See http://linux.die.net/man/3/sysconf

Of more interest is the amount of Virtual Memory (or swap space) which is configurable, up to the spare space on your HDD.

Also interesting is the Working Set Size. This is the amount of RAM (not swap) that is currently being used by your program. It changes constantly as the OS swaps pages in and out.

The question to ask is "Why do you want to know?". What are you trying to achieve?

That does not work because you could potentially increment the pointer outside of the system memory. There is no inherent restriction on that. Besides, memory models are more complicated than just long blocks of accessible addresses when you factor in paging, etc..

There is no one shot method to get the size of the memory on a system.

So first of all 0 does not mean , memory location at 0 position(basically it means null) , and there is difference between logical and physical address .We cann't access that way in user space program, however kernel may do that . So in user program we cannot access any memory location , that would result in trap . Page table maintained by operating system maps logical and physical address .So access address outside scope results in interrupt generation .

I dont know what Memory are you talking about .
But there is an easy way to find out the HDD memory size ..
If developing on Windows OS ,Use WMI , It is particulary designed to obtain system data in well Modelled fashion .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM