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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:55:08+00:00 2026-06-15T15:55:08+00:00

Using what I have learned here: How to use realloc in a function in

  • 0

Using what I have learned here: How to use realloc in a function in C, I wrote this program.

int data_length; // Keeps track of length of the dynamic array.
int n; // Keeps track of the number of elements in dynamic array.

void add(int x, int data[], int** test)
{

    n++;

    if (n > data_length)
    {
        data_length++;
        *test = realloc(*test, data_length * sizeof (int));
    }

    data[n-1] = x;

}

int main(void)
{

    int *data = malloc(2 * sizeof *data);

    data_length = 2; // Set the initial values.
    n = 0;

    add(0,data,&data);
    add(1,data,&data);
    add(2,data,&data);

    return 0;
}

The goal of the program is to have a dynamic array data that I can keep adding values to. When I try to add a value to data, if it is full, the length of the array is increased by using realloc.

Question

This program compiles and does not crash when run. However, printing out data[0],data[1],data[2] gives 0,1,0. The number 2 was not added to the array.

Is this due to my wrong use of realloc?

Additional Info

This program will be used later on with a varying number of “add” and possibly a “remove” function. Also, I know realloc should be checked to see if it failed (is NULL) but that has been left out here for simplicity.

I am still learning and experimenting with C. Thanks for your patience.

  • 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-15T15:55:09+00:00Added an answer on June 15, 2026 at 3:55 pm

    Your problem is in your utilisation of data, because it points on the old array’s address. Then, when your call realloc, this area is freed. So you are trying to access to an invalid address on the next instruction: this leads to an undefined behavior.

    Also you don’t need to use this data pointer. test is sufficient.

    (*test)[n-1] = x;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just learned how to program in C++ this last week, and I wrote
So far I have learned about generating thread dump and heap dump using jstack
I learned of gist.github.com from Mozilla Ubiquity , and have been using it to
I have just learned how to use the SQLite database for local storage in
We're using this script -- http://jsfiddle.net/6t74T/4/ -- which we learned about in this terrific
I have a C# Web App (Using ASP.NET 2.0) and I want to use
Using camera with fixed position I have to track an object in motion and
While I was trying to refresh page contents dynamically using Ajax/JQuery, I have learned
First, I have researched this question a lot. I learned how to read from
I am using a SQLite3 database in development, and now have started to use

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.