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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:10:33+00:00 2026-05-28T17:10:33+00:00

I am building an array and I want it to be a fixed size

  • 0

I am building an array and I want it to be a fixed size so that as I read in a file it only stores the last 10 commands. The file seems to read in correctly and it looks right when I print it but for some reason my memory is not getting freed.
MAX is set to 1000
and historySize is read earlier from the user. I ran valgrind on my code and when the calls to these functions are commented out I do not have any leaks.

I have a char ** history under my #includes

Here is my code

void setupHistoryFile()
{
    char string[MAX];
    FILE *fp;
    int len;
    int pos = 0;
    fp = fopen(".ush_history","r");
    if(fp == NULL)
    {
        //create the file
        fp = fopen(".ush_history","w");
    }
    else
    {
        history = (char**)malloc(historySize * sizeof(char*));//setup history file
        fgets(string,MAX,fp);
        len = strlen(string);
        if(string[len-1]=='\n')
            string[len-1]='\0';
        while(!feof(fp))
        {
            if(history[pos] != NULL)
            {
                free(history[pos]);
                history[pos]=NULL;
            }
            history[pos] = (char*)malloc((strlen(string)+1) * sizeof(char));
            //printf("Should be copying %s\n",string);          
            strcpy(history[pos], string);           
            pos++;
            pos = pos % historySize;
            fgets(string,MAX,fp);
            len = strlen(string);
            if(string[len-1]=='\n')
                string[len-1]='\0';
        }
    }
    fclose(fp);
}

I do have a function that cleans the history and it looks like this

void cleanHistory()
{
    int i;
    if(history != NULL)
    {
        for(i=0;i<historySize;i++)
        {
            free(history[i]);
            history[i] = NULL;
        }
        free(history);
        history = NULL;
    }
}
  • 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-28T17:10:34+00:00Added an answer on May 28, 2026 at 5:10 pm

    When you allocate memory with malloc, the chunk of memory allocated is not initialized. That means that if you do something like history[pos] != NULL might actually be true even if you haven’t put anything there.

    To be certain that the allocated memory is initialized, either use calloc or memset.

    Edit
    To be more specific, this part of your code will behave badly:

    if(history[pos] != NULL)
    {
        free(history[pos]);
        history[pos]=NULL;
    }
    

    If you are not lucky, history[pos] will contain some old data, which means you will try to free something you have not allocated.

    As a small side-note, you should be looping while fgets doesn’t return NULL. As it is now you don’t check for errors from fgets. Something like this:

    while (fgets(...) != NULL)
    

    Then you don’t need the double calls to fgets, and you will stop looping on both error and end of file.

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

Sidebar

Related Questions

i need help building a function that display an associative array and i want
I have two values in an array that I want to have show up
Say I have an array of arrays in Ruby, [[100,300], [400,500]] that I'm building
I'm building an autoloader that extends the include_path. It takes an array, appends the
Often, I am building an array by iterating through some data, e.g.: my_array =
I´m building a jquery animation from a multidimensional array, and in the callback of
building a site using PHP and MySQL that needs to store a lot of
Building a website that has English & Japanese speaking users, with the Japanese users
Can I call a function when building an array in Flex 3? public function
I declare an NSArray in my code then building the array from another array.

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.