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

The Archive Base Latest Questions

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

This question is definitely related to this and it’s answer is what I based

  • 0

This question is definitely related to this and it’s answer is what I based my function off of.

char *get_next_line(FILE *fp) {
char ch = 0;
int CUR_MAX = 4095;
char *buffer = (char*) malloc(sizeof(char) * CUR_MAX); // allocate buffer.
char *temp = (char*) malloc(sizeof(char) * CUR_MAX); // allocate buffer.
int count = 0;
int length = 0;

while ((ch != '\n')) {
  if (ch == '\377') { return NULL; }
  if(count ==CUR_MAX) {
    CUR_MAX *= 2;
    count = 0;
    if ((temp = realloc(buffer, CUR_MAX)) != NULL) {
      buffer = temp;
      free(temp);
    }
  }
  ch = getc(fp);
  buffer[length] = ch;
  length++;
  count++;
}

For some reason, when reading in very large strings I’m faced with:
glibc detected – realloc() invalid next size.

Is there something I’m missing here?

Thanks!

  • 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-14T14:15:46+00:00Added an answer on June 14, 2026 at 2:15 pm

    I believe you’re accessing memory outside of your malloced/realloced region.

    Not setting count = 0 inside your while look should fix this.


    To try and elaborate…

    When starting the formula:

    count = 0
    length = 0
    CUR_MAX = 4095
    

    These will increment until they reach 4095. Once we reach 4095, we will have:

    count = 0
    length = 4096
    CUR_MAX = 8190
    

    We will then increment until count is 8190. Shorly before then, we have:

    count = 8100
    length = 12196
    CUR_MAX = 8190
    

    Your array is only 8190 in length, but you’re dereferencing buffer[12196], which is an invalid index.


    Also, you probably want to handle the else case for when temp == NULL. This probably can be handled by an assert or a big old failure. And don’t free(temp) inside your success side. That’s freeing the memory you just attempted to allocate. And is causing your new segmentation fault.

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

Sidebar

Related Questions

I realize there's no definitely right answer to this question, but when people talk
Okay this is definitely a n00b question but here goes. The way I understand
This isn't a code question for once, but it definitely has me confused. Basically,
This question is directly related to this SO question I posed about 15 minutes
This question is related to another question I wrote: Trouble using DOTNET from PHP.
I realize that this question is slightly server related and could be posted on
This is related to a previous question, and surprisingly, I couldn't find an example
Several variations of this question have been asked, and I've definitely learned from reading
This Question is Related to my earlier post here: CSS loading issue with Android
This question is not so much programming related as it is deployment related. I

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.