我想查询安装内存的大小。我使用了上面的代码:
void GetInstalledMemory( char * MemorySize )
{
memset( MemorySize, 0, sizeof( MemorySize ) );
MEMORYSTATUSEX statex;
statex.dwLength = sizeof( statex );
if ( !GlobalMemoryStatusEx( &statex ) ) strcpy( MemorySize, "N/A" );
else sprintf( MemorySize, "%I64d", statex.ullTotalPhys / 1024 / 1024 );
}//GetInstalledMemory
此代码的问题是,在 32 位 PC 下它显示 3240 Mb,在 64 位 PC 下它显示 3976 MB RAM,但是两台 PC 都安装了 4.0 GB RAM。有没有办法以某种方式获得安装内存的正确大小?
谢谢!
最佳答案
此代码可能有效,
#include <windows.h>
#include <stdio.h>
int main()
{
MEMORYSTATUSEX m;
m.dwLength = sizeof (m);
GlobalMemoryStatusEx (&m);
printf("Installed Memory size = %I64d KB\n", m.ullTotalPhys/1024);
return 0;
}
但是硬件访问函数不在标准 C 中。所以它们不便于携带。
更新
The problem with this code is, that under a 32 bit PC it shows 3240 Mb, and under a 64 bit PC it shows 3976 MB RAM, however both PC has 4.0 GB RAM installed.
这是因为32位机器不能寻址超过3Gb的内存。
关于c - 如何获得安装的内存大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8701769/