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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:41:41+00:00 2026-06-18T06:41:41+00:00

I am doing some learning with C, and am having trouble identifying a memory

  • 0

I am doing some learning with C, and am having trouble identifying a memory leak situation.

First, some code:


My main function:

#define FILE_NAME "../data/input.txt"

char * testGetLine( FILE * );
int testGetCount(void);

int main(void)
{
    int count = 0;
    FILE * fptr;

    if ((fptr = fopen(FILE_NAME, "r")) != NULL) {
        char * line;
        while ((line = testGetLine(fptr)) != NULL) {            

            printf("%s", line);
            free(line); count++;
        }

        free(line); count++;

    } else {
        printf("%s\n", "Could not read file...");
    }

    // testing statements
    printf("testGetLine was called %d times\n", testGetCount());
    printf("free(line) was called %d times\n", count);

    fclose(fptr);
    return 0;
}

and my getline function:

#define LINE_BUFFER 500

int count = 0;

char * testGetLine(FILE * fptr)
{
    extern int count;

    char * line;
    line = malloc(sizeof(char) * LINE_BUFFER);
    count++;

    return fgets(line, LINE_BUFFER, fptr);
}

int testGetCount(void) {
    extern int count;
    return count;
}

my understanding is that I would need to call free everytime I have called my testGetLine function, which I do. By my count, on a simple text file with four lines I need to call free 5 times. I verify that with my testing statements in the following output:

This is in line 01
Now I am in line 02
line 03 here
and we finish with line 04
testGetLine was called 5 times
free(line) was called 5 times

What I am having trouble with is, valgrind says that I alloc 6 times, and am only calling free 5 times. Here is truncated output from valgrind:

HEAP SUMMARY:
    in use at exit: 500 bytes in 1 blocks
  total heap usage: 6 allocs, 5 frees, 3,068 bytes allocated
500 bytes in 1 blocks are definitely lost in loss record 1 of 1
   at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
   by 0x4007A5: testGetLine (testGetLine.c:13)
   by 0x400728: main (tester.c:16)
LEAK SUMMARY:
   definitely lost: 500 bytes in 1 blocks
   indirectly lost: 0 bytes in 0 blocks
     possibly lost: 0 bytes in 0 blocks
   still reachable: 0 bytes in 0 blocks
        suppressed: 0 bytes in 0 blocks

I feel I am missing something with the memory management. Where is the 6th memory allocation that valgrind says I am using? and how should I free it?


Followup to implement Adrian’s answer

testGetLine adjustment:

char * testGetLine(FILE * fptr)
{
    extern int count;

    char * line;
    line = malloc(sizeof(char) * LINE_BUFFER);
    count++;

    if (fgets(line, LINE_BUFFER, fptr) == NULL) {
        line[0] = '\0';
    }

    return line;
}

main while loop adjustment:

while ((line = testGetLine(fptr))[0] != '\0') {            

    printf("%s", line);
    free(line); count++;
}

free(line); count++;
  • 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-18T06:41:42+00:00Added an answer on June 18, 2026 at 6:41 am

    fgets return description:

    On success, the function returns str. If the end-of-file is
    encountered while attempting to read a character, the eof indicator is
    set (feof). If this happens before any characters could be read, the
    pointer returned is a null pointer (and the contents of str remain
    unchanged). If a read error occurs, the error indicator (ferror) is
    set and a null pointer is also returned (but the contents pointed by
    str may have changed).

    When fgets doesn’t read anything it doesn’t return the char * that you used malloc on.

    Therefore, the malloc in your last call isn’t being freed. The statement after your while doesn’t work as you want.

    Solution: change your return and return line instead:

    char * testGetLine(FILE * fptr)
    {
        extern int count;
    
        char * line;
        line = malloc(sizeof(char) * LINE_BUFFER);
        count++;
        fgets(line, LINE_BUFFER, fptr);
        return line;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning my first functional programming language but having some trouble with the initial
I'm trying to build some iOS apps while learning and am having some trouble
I am learning c++, and I am having issues doing some newbie things. I
Just learning rails, developing first app and having trouble finding a straight answer to
I'm currently doing some last-measure optimizations, mostly for fun and learning, and discovered something
So I'm working on just a learning project to expose myself to doing some
hello i'm learning qt and i'm doing the folowing to add some widgets to
I'm a few days into learning Clojure and are having some teething problems, so
I am new to QT and I am doing some learning. I would like
i'm a .net guy doing some work/learning RoR. i have a handful of environment-specific

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.