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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:07:54+00:00 2026-06-07T08:07:54+00:00

Disclaimer: This is homework. I am attempting it and do not expect or want

  • 0

Disclaimer: This is homework. I am attempting it and do not expect or want anyone to do it for me. Just a few pointers (hehe) where I’m going wrong would be appreciated.

The homework requires me to create an int* array that holds 10 elements, and then attempt to insert a million ints into it. Each insertion checks if the array needs to be resized, and if it does, I increase it’s size so it can hold one more element.

When I insert 10,000 elements, it works fine, but if I try 100,000 elements, I get the following error:

*** glibc detected *** ./set2: realloc(): invalid old size: 0x00000000024dc010 ***

This is the code I’m running. I’ve commented it so it’s easily readable.

void main()
{
    //begin with a size of 10
    int currentsize = 10;
    int* arr = malloc(currentsize * sizeof(int));       
    int i;

    //initalize with all elements set to INT_MAX
    for(i = 0; i < currentsize; i++) {
        arr[i] = INT_MAX;
    }


    // insert random elements
    for(i = 0; i < 100000; i++) {
        currentsize = add(rand() % 100,arr,currentsize);
    }

    free(arr);
}

/*
    Method resizes array if needed, and returns the new size of the array
    Also inserts the element into the array
*/
int add(int x, int* arr, int size)
{
    //find the first available location 
    int newSize = size;
    int i;
    for(i = 0; i < size; i++) {
        if (arr[i] == INT_MAX)
            break;
    }

    if (i >= size) {
        //need to realloc
        newSize++;
        arr = realloc(arr, newSize * sizeof(int) );     
    }

    arr[i] = x;

    return newSize;
}
  • 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-07T08:07:56+00:00Added an answer on June 7, 2026 at 8:07 am

    The error is probably because you properly use realloc to change arr in the function add, but this modified value is lost when add returns. So the next call to add will receive the old, now bad value.

    Also I can’t understand why you’re using a the for loop to search. You know you want to add at the last element, so why search? Just reallocate the array and plug the new value in the new slot.

    Incidentally I’m pretty sure your teacher is trying to get you to see that reallocating for each member causes an asymptotic run time problem. Most implementations of realloc will do a lot of copying with this algorithm. This is why real programs grow the array size by a factor greater than one (often 1.5 or 2) rather than by fixed amounts.

    The usual idiom is to abstract the variable size array in a struct:

    typedef struct array_s {
      int *elts;
      int size;
    } VARIABLE_ARRAY;
    
    void init(VARIABLE_ARRAY *a)
    {
      a->size = 10;
      a->elts = malloc(a->size * sizeof a->elts[0]);
      // CHECK FOR NULL RETURN FROM malloc() HERE
    }
    
    void ensure_size(VARIABLE_ARRAY *a, size_t size) 
    {
      if (a->size < size) {
    
        // RESET size HERE TO INCREASE BY FACTOR OF OLD SIZE
        // size = 2 * a->size;
    
        a->elts = realloc(size * sizeof a->elts[0]);
        a->size = size;
    
        // CHECK FOR NULL RETURN FROM realloc() HERE
      }
    }
    
    // Set the i'th position of array a. If there wasn't
    // enough space, expand the array so there is.
    void set(VARIABLE_ARRAY *a, int i, int val)
    {
      ensure_size(a, i + 1);
      a->elts[i] = val;
    }
    
    void test(void)
    {
      VARIABLE_ARRAY a;
    
      init(&a);
    
      for (int i = 0; i < 100000; i++) {
        set(&a, i, rand());
      }
    
      ...
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Full disclaimer: this is not really a homework, but I tagged it as such
DISCLAIMER: This is not a real-world example. It is just a theoretical question of
Disclaimer : this is not a how to question. I would more like to
Disclaimer: This is for a homework assignment, but the question is not regarding the
Disclaimer: This is not homework. Also, I'm describing a simplified situation so bear with
Disclaimer: this is not a question about how to install asp.net or an application
Disclaimer: This is for a programming class, but it is not the answer or
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial
DISCLAIMER: This question was not meant to be argumentative! What is fastest and less
Disclaimer: this question is purely informational and does not represent an actual problem 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.