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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:59:01+00:00 2026-06-07T19:59:01+00:00

Is it necessary to free memory before using realloc again for a pointer variable.

  • 0

Is it necessary to free memory before using realloc again for a pointer variable. Which of the following is correct?

for(i = 0; i < n; i++){
   myArray = (int *)realloc(myArray, i*sizeof(int));
}

for(i = 0; i < n; i++){
   myArray = (int *)realloc(myArray, i*sizeof(int));
   free(myArray);
   myArray = NULL;
}
  • 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-07T19:59:03+00:00Added an answer on June 7, 2026 at 7:59 pm

    The specific usefulness of realloc is that you don’t need to free before using it: it exists to grow memory that has already been allocated.

    So it is not required and would be uncommon. When passed a NULL pointer, realloc behaves as malloc. If you’re using free before calling it, you might as well be using malloc.

    Neither example is correct since you’ve omitted error handling. All the allocators can return NULL and the usage of realloc is a little tricky in this respect. Read the docs and examples carefully. Specifically, ptr = realloc(ptr, ... is always a bad idea because if realloc fails and returns NULL, then you’ve just lost your reference and leaked memory. Instead use a tmp variable, e.g.:

    tmp = realloc(ptr, newSize);
    if (tmp != NULL)
        ptr = tmp;
    else handle_error();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using realloc is the memory automatically freed? Or is it necessary to use
In C++ using delete to free memory obtained with malloc() doesn't necessarily cause a
I know from reading parashift.com that using delete on a pointer frees the memory
Is it necessary to check for nullptr after allocating an object using gcnew?
Since C# uses Garbage Collection. When is it necessary to use .Dispose to free
The following is designed to take a variable length constant char and print it
I have an application that keeps using up more and more memory as time
Consider a standard iterator where it is necessary to allocate memory for traversing a
I am juggling with two ways of free() 'ing malloc() 'ed memory in a
When using mmap() for shared memory (from Linux, or other UNIX-like systems) is it

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.