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 8467479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:39:27+00:00 2026-06-10T15:39:27+00:00

After running the program in valgrind -v it shows me that there are 3

  • 0

After running the program in valgrind -v it shows me that there are 3 unfreed blocks of memory (50 allocs, 47 frees). I’m probably failing to free infile outfile and temp – I guess. But when I put:

else {
        free(line);
            fclose(infile);  /* added lines */
            fclose(outfile); /* added lines */
            free(temp);      /* added lines */
        exit(EXIT_FAILURE);
} 

it doesn’t compile, showing me errors about undefined use of temp and outfile.

EDIT:
I changed it to (in lineRead):

else {
        free(line);
            fclose(infile);
            return NULL;
} 

and added following error catcher after while in main:

if ((check = readline(infile)) == NULL) {
    fclose(outfile);
}   

However, this gives me even more errors. Why is that?

/EDIT


How to fix that? I though that exit() does all the cleaning needed…

The code is changed in [1] because I wanted to simulate that particular error.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char* lineRead(FILE* infile)
{
    char* line = NULL;
    char* newbuf = NULL;
    int c;
    size_t bufsize = 0;
    size_t size = 0;
    while((c=fgetc(infile)) != EOF) {
        if (size >= bufsize) {
        if (bufsize == 0)
                bufsize = 2;
        else if (bufsize <= ((size_t)-1)/2)
                bufsize = size+1;
        else {
                free(line);
                exit(EXIT_FAILURE);
        }
        newbuf = realloc(line,bufsize);
        if (!newbuf) {
                free(line);
                exit(EXIT_FAILURE);
        } else {
        line = newbuf;
            }
        }
        if (c != '\n') {
            line[size++]=c;
        }

    }

    if(size >= bufsize) {
        if (size > (size_t)-1)      /* [1] I know that there should be*/
                            /* '<', but it is '>' just for testing errors */
        bufsize = size + 1;
        else {
        free(line);
        exit(EXIT_FAILURE);
    }
        newbuf = realloc(line,bufsize);
        if (!newbuf) {
        free(line);
        exit(EXIT_FAILURE);
        }
        line = newbuf;
    }
    line[size++]='\0';
    return line;
}

int main(int argc, char* argv[])
{

 char *line=NULL; 
 char **lines=NULL; 
 int linenumber=0;
 int c;
 void *temp=NULL;

while((line=lineRead(infile))!=NULL) {
    linenumber++;
    temp=realloc(lines, (linenumber)*sizeof(char*));
    if(temp==NULL) {
    printf("Bad alloc error\n");
    free(lines);
    return 0;
    } else {
    lines=temp;
    }

}
/* processing lines */
free(lines);


return 0;
}
  • 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-10T15:39:28+00:00Added an answer on June 10, 2026 at 3:39 pm

    You didn’t free

    newbuf // in read line
    line // in main
    temp // Which gets free'd only on after bad malloc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After running the program, Logcat shows some errors (picture). But after that program runs
I'm running valgrind 3.5.0 to try and squash memory leaks in my program. I
After running for a number of hours on Linux, my Python 2.6 program that
Hi, there is a Java program edited and compiled using Netbeans 6.9.1. After running
I want to run my C++ program after flushing the cache, Before running my
After running the following SQL statements, you will see that, MySQL has automatically created
After running exec sp_lock on one of our databases we found there's close to
I'm having a memory leak issue in a service program that runs SQL scripts
I am running a program in Go which sends data continuously after reading a
I have successfully done this before but now after running the program numerous times

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.