c语言 查看系统内存,C/C++获取系统物理内存大小

该篇博客介绍了如何在不同操作系统平台上使用C语言获取系统物理内存的大小。通过调用特定接口,如Windows下的`GlobalMemoryStatusEx`,或者UNIX变种系统下的`sysctl`或`sysconf`,实现了跨平台的内存查询功能。
摘要由CSDN通过智能技术生成

1、各平台调用接口

64dd315a719cad3e02c5112b2e43e841.png

2、接口实现#if defined(_WIN32)

#include

#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))

#include

#include

#include

#if defined(BSD)

#include

#endif

#else

#error "Unable to define getMemorySize( ) for an unknown OS."

#endif

size_t getMemorySize( )

{

#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__))

/* Cygwin under Windows. ------------------------------------ */

/* New 64-bit MEMORYSTATUSEX isn't available. Use old 32.bit */

MEMORYSTATUS status;

status.dwLength = sizeof(status);

GlobalMemoryStatus

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值