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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:48:42+00:00 2026-06-14T18:48:42+00:00

I’m writing a variable list implementation. In my insertion step, I check if the

  • 0

I’m writing a variable list implementation. In my insertion step, I check if the array’s size is maxed out, and if so I double the maximum capacity and call realloc() to allocate me some new memory. Going from size 2 to 4, 4 to 8, and 8 to 16 works fine, but going from size 16 to 32 gives me some random zeroes in my array. Can anyone tell me what’s going on here? I know I could avoid using realloc() by mallocing some new space, using memcpy and then freeing the old pointer… and perhaps there’s no performance hit from doing that. But my intuition tells me that there is, and in any case I thought that’s what realloc was there for. Can anyone tell me what’s going on? The key function in this code is the append function.

#include <stdio.h>
#include <stdlib.h>
#include "problem5.h"
#define FAILURE -1
#define SUCCESS 0

ArrayList ArrayList_Init(int n, int (*append) (ArrayList, int), void (*print) (ArrayList), void (*insert) (ArrayList, int, int), void (*destroy) (ArrayList), int (*valueOf) (ArrayList, int)) 
{
    ArrayList newArrayList = (ArrayList) malloc(sizeof(ArrayList_));
    newArrayList->max_size = n;
    newArrayList->current_size = 0;
    newArrayList->data = malloc(n*sizeof(int));

    newArrayList->append = append;
    newArrayList->destroy = destroy;
    newArrayList->print = print;
    newArrayList->insert = insert;
    newArrayList->valueOf = valueOf;

    return newArrayList;
}// init a new list with capacity n

int append_(ArrayList list, int val)
{
    //if the array is at maximum capacity
        //double the capacity
        //update max_size
    //insert the value in the first open spot in the array (aka index current_size)
    //increment current_size
    if (list->current_size == list->max_size) {
        list->max_size *= 2;
        if (( list->data = realloc(list->data, list->max_size) ) == NULL)
            return FAILURE;
    }
    list->data[list->current_size] = val;
    list->current_size++;
    return SUCCESS;
}

void print_(ArrayList list)
{
    int i;
    printf("List of size %d, max size %d. Contents:\n", list->current_size, list->max_size);
    for (i=0; i<list->current_size; i++)
        printf("%d, ", list->data[i]);
    printf("\n");
}

void insert_(ArrayList list, int val, int index) {

}// insert val into index
void destroy_(ArrayList list)
{
    //free list memory
}
int valueOf_(ArrayList list, int index)
{
    //return value of specified element
    return 0;
}

int main()
{
    ArrayList list;
    int stat, count = 0;

    list = ArrayList_Init(2, append_, print_, insert_, destroy_, valueOf_); // init a new list with capacity 8
    do {
        printf("Appending %d\n", count);
        stat = list->append(list, count) ; // add val to end of the list
        list->print(list);
    } while (stat == SUCCESS && ++count < 20);

    return 0;
}

And here’s the output of this:

Appending 0
List of size 1, max size 2. Contents:
0, 
Appending 1
List of size 2, max size 2. Contents:
0, 1, 
Appending 2
List of size 3, max size 4. Contents:
0, 1, 2, 
Appending 3
List of size 4, max size 4. Contents:
0, 1, 2, 3, 
Appending 4
List of size 5, max size 8. Contents:
0, 1, 2, 3, 4, 
Appending 5
List of size 6, max size 8. Contents:
0, 1, 2, 3, 4, 5, 
Appending 6
List of size 7, max size 8. Contents:
0, 1, 2, 3, 4, 5, 6, 
Appending 7
List of size 8, max size 8. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 
Appending 8
List of size 9, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 
Appending 9
List of size 10, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
Appending 10
List of size 11, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
Appending 11
List of size 12, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
Appending 12
List of size 13, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
Appending 13
List of size 14, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
Appending 14
List of size 15, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
Appending 15
List of size 16, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
Appending 16
List of size 17, max size 32. Contents:
0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 
Appending 17
List of size 18, max size 32. Contents:
0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 
Appending 18
List of size 19, max size 32. Contents:
0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 18, 
Appending 19
List of size 20, max size 32. Contents:
0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 18, 19, 
  • 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-14T18:48:43+00:00Added an answer on June 14, 2026 at 6:48 pm

    It is very bad to write so:

    list->data = realloc(list->data, list->max_size)
    

    You should use new variable and if memory was reallocated you write so:

    List->data = temp;
    

    It will protect you from memory leak.

    And you forgot *sizeof(int)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am writing an app with both english and french support. The app requests
I have a reasonable size flat file database of text documents mostly saved in
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.