Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:25:41+00:00 2026-05-28T00:25:41+00:00

Using C++, is there a way I could get basic information about the computer?

  • 0

Using C++, is there a way I could get basic information about the computer?

For example, is there a way I could check how much memory is being used (by the whole computer not just my computer), the total memory available, virtual memory usage, CPU usage, networks stats and so on?

I am using Mac OS X v10.6 (Snow Leopard), but I would prefer a solution that could be implemented for all Mac OSs (for example, Mac OS X v10.7 (Lion)).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:25:42+00:00Added an answer on May 28, 2026 at 12:25 am

    For system-wide memory usage information under Mac OS X, open and read the file /usr/bin/vm_stat. Something like this:

    static double ParseMemValue(const char * b)
    {
        while((*b)&&(isdigit(*b) == false))
            b++;
        return isdigit(*b) ? atof(b) : -1.0;
    }
    
    // Returns a number between 0.0f and 1.0f, with 0.0f meaning all RAM is available, and 1.0f meaning all RAM is currently in use
    float GetSystemMemoryUsagePercentage()
    {
        FILE * fpIn = popen("/usr/bin/vm_stat", "r");
        if (fpIn)
        {
            double pagesUsed = 0.0, totalPages = 0.0;
            char buf[512];
            while(fgets(buf, sizeof(buf), fpIn) != NULL)
            {
                if (strncmp(buf, "Pages", 5) == 0)
                {
                    double val = ParseMemValue(buf);
                    if (val >= 0.0)
                    {
                        if ((strncmp(buf, "Pages wired",  11) == 0) ||
                            (strncmp(buf, "Pages active", 12) == 0)
                           )
    
                            pagesUsed += val;
    
                        totalPages += val;
                    }
                }
                else
                  if (strncmp(buf, "Mach Virtual Memory Statistics", 30) != 0)
                      break; // Stop at "Translation Faults". We don't care
                             // about anything at or below that
            }
            pclose(fpIn);
    
            if (totalPages > 0.0)
                return (float) (pagesUsed/totalPages);
        }
        return -1.0f;  // Indicate failure
    }
    

    For a CPU usage indicator, do something like this:

    #include <mach/mach_init.h>
    #include <mach/mach_error.h>
    #include <mach/mach_host.h>
    #include <mach/vm_map.h>
    
    static unsigned long long _previousTotalTicks = 0;
    static unsigned long long _previousIdleTicks = 0;
    
    // Returns 1.0f for "CPU fully pinned", 0.0f for "CPU idle", or somewhere in between
    // You'll need to call this at regular intervals, since it measures the load between
    // the previous call and the current one.
    float GetCPULoad()
    {
        host_cpu_load_info_data_t cpuinfo;
        mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
        if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&cpuinfo, &count) == KERN_SUCCESS)
        {
            unsigned long long totalTicks = 0;
            for(int i=0; i<CPU_STATE_MAX; i++)
                totalTicks += cpuinfo.cpu_ticks[i];
            return CalculateCPULoad(cpuinfo.cpu_ticks[CPU_STATE_IDLE], totalTicks);
        }
        else
           return -1.0f;
    }
    
    float CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks)
    {
        unsigned long long totalTicksSinceLastTime = totalTicks-_previousTotalTicks;
        unsigned long long idleTicksSinceLastTime  = idleTicks-_previousIdleTicks;
        float ret = 1.0f-((totalTicksSinceLastTime > 0) ? ((float)idleTicksSinceLastTime)/totalTicksSinceLastTime : 0);
        _previousTotalTicks = totalTicks;
        _previousIdleTicks  = idleTicks;
        return ret;
    }
    

    For network statistics, I don’t know the solution (other than maybe to run netstat and parse the results somehow… it depends on what network statistics you are interested in I suppose).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way using the iPhone SDK to get WiFi information? Things like
I could do this using loops, but is there a way to take two
Is there a way using SWT to get a list of all processes currently
Is there a way using Subversion to get snapshot of the files for a
From the native Win32 API using C++ is there a way to determine whether
Is there a way using Java to over-ride the browser authentication dialog box when
Is there a way using Flash (CS3+AS3) to determine if the published swf is
Is there a way using SQL to list all foreign keys for a given
Is there a way using Core Audio on OS X to extract a set
Is there a way using JNI and C# code to create a program that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.