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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:46:22+00:00 2026-05-17T15:46:22+00:00

When we reallocate memory via realloc() , are the previous contents over-written? I am

  • 0

When we reallocate memory via realloc(), are the previous contents over-written? I am trying to make a program which reallocates memory each time we enter the data into it.

Please tell me about memory allocation via realloc, is it compiler dependent for example?

  • 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-17T15:46:22+00:00Added an answer on May 17, 2026 at 3:46 pm

    Don’t worry about the old contents.

    The correct way to use realloc is to use a specific pointer for the reallocation, test that pointer and, if everything worked out ok, change the old pointer

    int *oldpointer = malloc(100);
    
    /* ... */
    
    int *newpointer = realloc(oldpointer, 1000);
    if (newpointer == NULL) {
        /* problems!!!!                                 */
        /* tell the user to stop playing DOOM and retry */
        /* or free(oldpointer) and abort, or whatever   */
    } else {
        /* everything ok                                                                 */
        /* `newpointer` now points to a new memory block with the contents of oldpointer */
        /* `oldpointer` points to an invalid address                                     */
        oldpointer = newpointer;
        /* oldpointer points to the correct address                                */
        /* the contents at oldpointer have been copied while realloc did its thing */
        /* if the new size is smaller than the old size, some data was lost        */
    }
    
    /* ... */
    
    /* don't forget to `free(oldpointer);` at some time */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying reallocate more 256 bytes to buffer on each loop call. In this
I am trying to dynamically reallocate memory for an array of structs (actually an
realloc is used to reallocate the memory dynamically. Suppose I have allocated 7 bytes
I'm trying to free memory that's reallocated but I get an error... float *
Does the destructor deallocate memory assigned to the object which it belongs to or
What is the right and best way to reallocate memory? for example I allocate
Some time ago a friend of mine told me not to use realloc because
I have in my program a struct type called Square which is used to
In C the standard memory handling functions are malloc() , realloc() and free() .
I am trying to get a handle on proper memory usage and garbage collection

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.