c - 如何获得安装的内存大小?

标签 c windows

我想查询安装内存的大小。我使用了上面的代码:

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 中。所以它们不便于携带。

MSDN

更新

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/

相关文章:

交叉编译的二进制文件不存在?

c++ - 如何将纪元时间转换为年份

c - 数组中的输出崩溃

c++ - 一个线程写入变量,另一个线程读取该变量,我如何(C++11 之前)保护该变量?

c - 如何在 C 中使用链表进行大整数乘法运算?

编译错误: 'expected expression' while editing Linux pre-processor statment

java - 在 Java 中获取我的文档路径

android - 使用 aapt 工具读取 apk 文件的较少信息

c++ - Windows 10 是否会防止您访问其他程序正在使用的内存?

javascript - Firefox 15 中的 ajaking onbeforeunload