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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:31:57+00:00 2026-06-15T11:31:57+00:00

Any idea why I’m getting the error, realloc(): invalid next size ? while (loc)

  • 0

Any idea why I’m getting the error, realloc(): invalid next size ?

  while (loc) {
    char nextLine[MAX_PATH_LEN + 30];
    sprintf(nextLine, "<p>%s:%d</p>", loc->item.pathname,
    loc->item.offset);
    DPRINTF('h', ("Next line is: %s, length: %d\n", nextLine, strlen(nextLine))\
);

    DPRINTF('h', ("spaceUsedUp is %d\n", spaceUsedUp));
    while (spaceUsedUp + strlen(nextLine) > allocatedSize) {
      printf("Allocated size is now %d\n", allocatedSize);
      allocatedSize *= 2;
    }

    DPRINTF('h', ("Will realloc size %d\n", allocatedSize));
    char *tmp = (char *)realloc(result, allocatedSize);
    DPRINTF('h', ("Done with the realloc.\n"));
    fflush(stdout);
    if (tmp == NULL) {
      perror("realloc");
    }
    result = tmp;
    tmp = NULL;

    int theOne = (spaceUsedUp == 0) ? 0 : 1;
    memcpy(result+spaceUsedUp-theOne, nextLine, sizeof(nextLine));

    spaceUsedUp += strlen(nextLine) - theOne;
    loc = loc->nextLocation;
  }

The output is:

Next line is: <p>/dirA/f3:6162</p>, length: 20
spaceUsedUp is 0
Will realloc size 100
Done with the realloc.
Next line is: <p>/dirA/f3:6038</p>, length: 20
spaceUsedUp is 19
Will realloc size 100
*** glibc detected *** ./proj3/disksearch: realloc(): invalid next size: 0x0000000001797fa0 ***
  • 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-15T11:31:58+00:00Added an answer on June 15, 2026 at 11:31 am

    It’s because you memcpy sizeof(nextLine) bytes, clobbering outside the memory you have allocated. Instead of sizeof(nextLine) you should use strlen(nextLine) and things should be fine.

    Realloc errors like this generally indicate that the memory heap has been corrupted, which usually occurs because of out-of-bounds writes or reusing freed pointers.

    Valgrind is your friend for problems like these.

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

Sidebar

Related Questions

Any idea about cp: cycle detected: Error on Solaries. I am getting this while
Any idea why I'm getting this error at runtime ? I'm trying to deploy
Any idea why I am getting this exception? Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean
Any idea about ORA-1555: snapshot too old: rollback segment number I am getting this
Any idea where the setting is hiding for turning gridlines off while using excel
Any idea why I get this error: Warning: Missing argument 1 for person::__construct(), called
Any idea on how to save an array of unsigned char to a field
Any idea what's the code for getting text with every letter in bubble like
Any idea why my function isn't getting injected into the web page? I'm pretty
Any idea on how can I track users bookmarking event? I want to find

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.