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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:57:10+00:00 2026-06-01T17:57:10+00:00

I would like to resize the array when it reaches its max capacity. But

  • 0

I would like to resize the array when it reaches its max capacity. But error came up after i do ./a.out Please help me…

Error: a.out: malloc.c:3574: mremap_chunk: Assertion `((size + offset) & (mp_.pagesize-1)) == 0' failed.

code:

#include<stdio.h>
#include <stdlib.h>


int main(void)
{
  int cap=5;
  int *arr = malloc(cap*sizeof(int));
  FILE *f;

  if((f=fopen("/home/file.txt","r"))==NULL)
    printf("You cannot open");  

  while(fscanf(f, "%d", arr++)!=EOF)
  {
    index++;
    if(index==cap-1)
      arr = realloc(arr, (cap +=1) * sizeof(int));
  } 

  return 0;
}
  • 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-01T17:57:10+00:00Added an answer on June 1, 2026 at 5:57 pm

    You have arr++ in your loop condition. That means arr doesn’t point to the start of the allocated memory anymore when you call realloc(). That’s going to end up with the error you’re seeing.

    Also:

    1. Programming safety note:

      Don’t call realloc() in the form:

      foo = realloc(foo, bar);
      

      If an error occurs, foo will be set to NULL and you’ll leak the original allocation.

    2. Nonidiomatic code note:

      (cap +=1) * sizeof(int)
      

      is a bit weird. Why not ++cap * sizeof(int)? Or better yet, do it on two lines rather than cramming it all into one.

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

Sidebar

Related Questions

I would like to resize a Java BufferedImage, making it smaller vertically but without
I have two resize functions that I would like to perform one after the
I would like manipulate/resize images in a similar way to Pinterest but I am
I'm doing resize on jpg, after that i would like to get md5 from
I have a directory full of images that I would like to resize to
I would like to have a resize indication on a windows forms Form (the
Would like to know how to hide an div after a set of css3
I would like to append a value at the end of my numpy.array .
I would like to resize the topmost windows. I have small problem in the
I would like to resize images on server side to make thumbnail dynamicaly. I'm

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.