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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:36:03+00:00 2026-06-14T22:36:03+00:00

I have to dynamically allocate array of words. Words are stored in a file

  • 0

I have to dynamically allocate array of words. Words are stored in a file separated by variable count of white-space characters. I don’t know how many words is in the file a they can have variable length.

I have this code:

void readWord(FILE* stream, char *word, char first_c) {

    word[0] = first_c;
    char val;
    int wlen = 1;
    // isWhitespac is my function - tests if char is blank or '\n'
    while ((val = fgetc(stream)) != EOF && isWhitespace(val) == 0) {
        wlen++;
        word = realloc(word, (wlen+1) * sizeof (char));

        word[wlen-1] = val;    

    }
    word[wlen] = '\0';
}

int readList(const char *file) {

    FILE* f;
    char **arr;
    char val;
    int wcount = 0;

    arr = malloc(sizeof (char*));
    f = fopen(file, "r");

    while (fscanf(f, " %c", &val) == 1) {
        wcount++;
        arr = realloc(arr, wcount * sizeof (char *));

        arr[wcount - 1] = malloc(sizeof (char));  

        readWord(f, arr[wcount-1], val); 
        printf("%s\n", arr[wcount-1]);

    }


    for (int i = 0; i < wcount; ++i) {
        free(arr[i]);
    }

    free(arr);

    fclose(f);
    return 0;
}

It appears to work fine, it reads a prints all the words. But when I run the program with Valgrind the are too many errors, which I can’t find. Could anyone help me? (I know I have to test if malloc and others went fine, it is just a test func.)

The Valgrind log is quite long, should I post it too?

  • 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-14T22:36:04+00:00Added an answer on June 14, 2026 at 10:36 pm

    One of the issues is that you do realloc inside readWord. If realloc allocates a new buffer and doesn’t just extend the current one then your code will crash (you will double free the pointer) and this is what Valgrind picks up. To fix this I would rewrite the code so it returns a pointer instead of void.

    char * readWord(FILE* stream, char *word, char first_c) {
        word[0] = first_c;
        char val;
        int wlen = 1;
        // isWhitespac is my function - tests if char is blank or '\n'
        while ((val = fgetc(stream)) != EOF && isWhitespace(val) == 0) {
          wlen++;
          word = realloc(word, (wlen+1) * sizeof (char));
          word[wlen-1] = val;
        }
        word[wlen] = '\0';
        return word;
    }
    

    And then change the loop in readList to this:

    while (fscanf(f, " %c", &val) == 1) {
      wcount++;
      arr = realloc(arr, wcount * sizeof (char *));
      arr[wcount-1]=malloc(sizeof(char));
      arr[wcount - 1] = readWord(f, arr[wcount-1],  val);
      printf("%s\n", arr[wcount-1]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code to dynamically allocate an array of 100 doubles to a pointer
I have c++ code that attempts to dynamically allocate a 2d array of bytes
I have a dynamically allocated array : myRectangle lastRectanglesArray = new myRectangle[lastMaxLabel]; I would
This question is in C++. I am trying to dynamically allocate an array of
In other words, what options do I have to allocate memory in JavaScript? I
I am trying to dynamically allocate an array of base(Student) classes, and then assign
I have an array of structs that I have defined in a header file:
This is the code that I have to dynamically declaring an array inside a
Before variable-length arrays were supported, I would dynamically allocate them like this: int foo(size_t
I need some help with allocating arrays dynamically. I have an array whose size

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.