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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:31:30+00:00 2026-05-26T00:31:30+00:00

I’m trying to implement dynamic array in C using realloc(). My understanding is that

  • 0

I’m trying to implement dynamic array in C using realloc(). My understanding is that realloc() preserves old contents of the memory block the old pointer points to, but my following testing code suggests otherwise:

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

int DARRAYSIZE=5;

typedef struct dynamicArray{
    int size;
    int *items;
}DArray;

int init(DArray *DAP){ //initialise the DArray
    DAP->items=(int *)malloc(sizeof(int)*DARRAYSIZE);
    if(DAP->items){
        DAP->size=0;
        return 0;
    }else{
        printf("Malloc failed!\n");
        return 1;
    }
}

void print(DArray *DAP){ //print all elements in the DArray
    int i=0;

    for(;i<DAP->size;i++)
        printf("%d\t",DAP->items[i]);

    printf("\n");
}

void add(DArray *DAP,int val){ //add the new val into the DArray
    if(!full(DAP)){
        DAP->items[DAP->size++]=val;    
    }else{
        if(!grow(DAP)){
            DAP->items[DAP->size++]=val;    
        }else
            exit(1);
    }
}

int full(DArray *DAP){ //returns 1 if the DAarray is full
    if(DAP->size==DARRAYSIZE)
        return 1;
    else
        return 0;
}

int grow(DArray *DAP){ //grows the DArray to double its original size
    int *temp=(int *)realloc(DAP->items,DARRAYSIZE*2);
    if(!temp){
        printf("Realloc failed!\n");
        return 1;
    }else{
        DAP->items=temp;
        DARRAYSIZE*=2;
        printf("Darray doubled and current contents are:\n");
        print(DAP);
        return 0;
    }
}

void destroy(DArray *DAP){ //destroies the DArray
    free(DAP->items);
}

int main(void){
    DArray newDA;
    if(!init(&newDA)){
        int i;
        for(i=1;i<30;i++)
            add(&newDA,i);

    }else
        exit(1);

    destroy(&newDA);

    return 0;
}

What I did was print the contents of the array as soon as its size is doubled in function grow(). I compiled the code using:

:gcc -version
i686-apple-darwin11-llvm-gcc-4.2

and below is the output:

enter image description here

with the unexpected 0’s in the output.

Please kindly advise what I’m doing wrong 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-05-26T00:31:31+00:00Added an answer on May 26, 2026 at 12:31 am

    You forgot sizeof(int) in your realloc(), so you keep shrinking your array.

    You also need to keep track of the number of items in use and the amount of space allocated in the dynamic array structure; these are two separate measures, and both are needed. But you can’t use a global variable (currently DYNARRAYSIZE) to hold the size of every dynamic array. You need one per dynamic array.

    You also need to look at full(); it compares the size with DARRAYSIZE…always!


    Working Output

    Formatted with tabstops set at 3

    Darray doubled and current contents are:
    Max = 10; Cur = 5
    1  2  3  4  5
    Darray doubled and current contents are:
    Max = 20; Cur = 10
    1  2  3  4  5  6  7  8  9  10
    Darray doubled and current contents are:
    Max = 40; Cur = 20
    1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20
    

    Working Code

    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    enum { DARRAYSIZE = 5 };
    
    typedef struct dynamicArray
    {
        int max_size;
        int cur_size;
        int *items;
    } DArray;
    
    extern int  init(DArray *DAP);
    extern void add(DArray *DAP, int val);
    extern void destroy(DArray *DAP);
    extern void print(DArray *DAP);
    static int  full(DArray *DAP);
    static int  grow(DArray *DAP);
    
    //initialise the DArray
    int init(DArray *DAP)
    {
        DAP->items=(int *)malloc(sizeof(int)*DARRAYSIZE);
        if (DAP->items)
        {
            DAP->max_size = DARRAYSIZE;
            DAP->cur_size = 0;
            return 0;
        }
        else
        {
            printf("Malloc failed!\n");
            return 1;
        }
    }
    
    //print all elements in the DArray
    void print(DArray *DAP)
    {
        printf("Max = %d; Cur = %d\n", DAP->max_size, DAP->cur_size);
        for (int i = 0; i < DAP->cur_size; i++)
            printf("%d\t", DAP->items[i]);
        printf("\n");
    }
    
    //add the new val into the DArray
    void add(DArray *DAP, int val)
    {
        if (!full(DAP))
            DAP->items[DAP->cur_size++] = val;    
        else if (!grow(DAP))
            DAP->items[DAP->cur_size++] = val;    
        else
            exit(1);
    }
    
    //returns 1 if the DAarray is full
    static int full(DArray *DAP)
    {
        assert(DAP->cur_size >= 0 && DAP->max_size >= 0);
        assert(DAP->cur_size <= DAP->max_size);
        if (DAP->cur_size == DAP->max_size)
            return 1;
        else
            return 0;
    }
    
    //grows the DArray to double its original size
    static int grow(DArray *DAP)
    {
        int *temp=(int *)realloc(DAP->items, sizeof(*temp) * DAP->max_size * 2);
        if (!temp)
        {
            printf("Realloc failed!\n");
            return 1;
        }
        else
        {
            DAP->items = temp;
            DAP->max_size *= 2;
            printf("Darray doubled and current contents are:\n");
            print(DAP);
            return 0;
        }
    }
    
    //destroys the DArray
    void destroy(DArray *DAP)
    {
        free(DAP->items);
        DAP->items = 0;
        DAP->max_size = 0;
        DAP->cur_size = 0;
    }
    
    int main(void)
    {
        DArray newDA;
        if (!init(&newDA))
        {
            for (int i = 1; i < 30; i++)
                add(&newDA, i);
        }
        else
            exit(1);
    
        destroy(&newDA);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.