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

  • SEARCH
  • Home
  • 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 8904823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:14:07+00:00 2026-06-15T02:14:07+00:00

I have written a program, However I have discovered it has a memory leak

  • 0

I have written a program, However I have discovered it has a memory leak and after a few iterations it segfaults. I don’t understand where this memory leak is.

Here is the entire code snippet below. I’m aware there are conventional errors with the code, like the global allocation and making a buffer that is way larger than it needs to be. I just can’t see the source of the leak.

Thanks in advance.

#include <stdlib.h>
#include <stdio.h>

#define DATA 1000
#define TRUE 1
#define FALSE 0
const int16_t TIME = 600; // Time in seconds.
const int8_t DEBUG = 0;
// Globals Because I'm lazy!
long int cpua[5];
long int cpub[5];
long int mema[2];
long int nets[2];
long int netr[2];
int k=0;
char ch[150];
FILE *file_read;
char buffer[DATA];

void CPUread(void){
    file_read = popen("cat /proc/stat | grep \"cpu \"","r");
    fgets(buffer, DATA, file_read);
    sscanf(buffer,"%s %ld %ld %ld %ld %ld",&ch[0],&cpua[0],&cpua[1],&cpua[2],&cpua[3],&cpua[4]);

    // Time to read the network..
    file_read = popen("ifconfig | grep -m 1 \"RX bytes:\" | awk -F \"[^0-9]*\" '{ print $2 }'","r");
    fgets(buffer, DATA, file_read);
    sscanf(buffer,"%ld",&netr[0]);
    file_read = popen("ifconfig | grep -m 1 \"RX bytes:\" | awk -F \"[^0-9]*\" '{ print $5 }'","r");
    fgets(buffer, DATA, file_read);
    sscanf(buffer,"%ld",&nets[0]);

    // Time to read memory
    file_read = popen("cat /proc/meminfo | grep -m 1 \"MemFree\" | awk -F \"[^0-9]*\" '{ print $2 }'","r");
    fgets(buffer, DATA, file_read);
    sscanf(buffer,"%ld",&mema[0]);

    file_read = popen("cat /proc/meminfo | grep -m 1 \"MemTotal\" | awk -F \"[^0-9]*\" '{ print $2 }'","r");
    fgets(buffer, DATA, file_read);
    sscanf(buffer,"%ld",&mema[1]);
}


void main(void){
    FILE *file_write, *fp;
    int i;
    long int total=0, ptotal=0;
    int valid = FALSE;
    char charbuf[100];
    file_write=popen("echo -n \"/var/www/dir/`date +%Y-%b-%d`.csv\"","r");
    fgets(charbuf,100, file_write);
    while(1){
        // Open up the status file container and read from the system.
        if(valid == FALSE ){

            CPUread();
            // Need to make a new file here.... With Date code.
            fp=fopen(charbuf,"a");
            if ( fp == NULL){
                perror("Dummy Spit In initial File create!\n");
                exit(-1);
            }
            fprintf(fp,"CPU,IOwait,Memory,KB_up,KB_down\n");
            if(DEBUG) printf("CPU,IOwait,Memory,KB_up,KB_down\n");
            valid = TRUE;
            fclose(fp);
        }else{
            for(i=0;i<5;i++) cpub[i] = cpua[i];
            nets[1]=nets[0];
            netr[1]=netr[0];
            CPUread();

            total = 0;
            for(i=0;i<5;i++){
                cpub[i]=cpua[i]-cpub[i];
                total+=cpub[i];
            }
            // Time to find the CPU useage and IO waits...
            fp=fopen(charbuf,"a");
            if ( fp == NULL){
                perror("Dummy Spit Trying to open file\n");
                exit(-1);
            }
            fprintf(fp,"%0.2f,%0.2f,%0.2f,%ld,%ld\n",(((float)cpub[0])/total)*100,(((float)cpub[4]/total)*100),((((float)mema[1]-(float)mema[0])/(float)mema[1]))*100,(nets[0]-nets[1])/1024,(netr[0]-netr[1])/1024);
            if(DEBUG) printf("%0.2f,%0.2f,%0.2f,%ld,%ld\n",(((float)cpub[0])/total)*100,(((float)cpub[4]/total)*100),((((float)mema[1]-(float)mema[0])/(float)mema[1]))*100,(nets[0]-nets[1])/1024,(netr[0]-netr[1])/1024);
            fclose(fp);
        }
    sleep(TIME);
    }
} 
  • 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-06-15T02:14:08+00:00Added an answer on June 15, 2026 at 2:14 am

    I believe you need to call pclose() on the descriptors returned from popen() in your CPUread() function.

    See the popen() documentation here:

    popen() man page on die.net

    I’m not sure this directly is the cause of your problem, but it is definitely a resource leak.

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

Sidebar

Related Questions

I have written php program and uploaded on server. I want run this program
I have written a program that uses qhttp to get a webpage. This works
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have written a program which will serialize and de-serialize, it does this fine
I have written a VB.Net program however there is a bug in one of
I have written a php program that generates rss feeds, however I am having
I have written a program that makes sure each cookie has more than 5
I have written a fairly large program in Fortran 90. It has been working
i have written the following program however every time i run it, the for
I have written a simple program using parallel python, and all works well. However,

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.