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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:30:04+00:00 2026-06-17T23:30:04+00:00

I know that realloc will free memory when necessary, and I know the third

  • 0

I know that realloc will free memory when necessary, and I know the third rule of C – “for every malloc there must be an equal and opposite free“… but how do these two work together?

The situation is best described in code:

int main()
{
    myStruct **myStructArray;
    int      i, num_elements;

    num_elements = getnumber(); // gets value for num_elements

    myStructArray = (myStruct **) malloc(num_elements * sizeof(myStruct*));
    for (i=0; i<num_elements; i++)
        myStructArray[i] = (myStruct *) malloc(sizeof(myStruct));

    // so far so good...

    num_elements = getnumber(); // gets new, LOWER value

    myStructArray = realloc(myStructArrary, num_elements * sizeof(myStruct*));

    // rest_of_code, and necessary free loop for myStructArray etc...
}

Obviously the above is nothing more than a snippet, but a snippet paints a thousand words.

Would this create a memory leak? I know the call to realloc will free the pointers’ memory, but I can see arguments for and against the possibility that there is still going to be a bunch of memory forgotten about.

A leak can be circumvented by incorporating int number_elements_new into the code and loop free-ing the surplus myStructs before calling realloc to free the (now NULL) pointers.

If realloc does the donkeywork and frees up ALL the associated memory that’s great, otherwise I’ve got to trawl through to make sure nothing has been missed – myStruct itself contains allocated memory and so on.

Thank you for your recommendations…

  • 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-17T23:30:05+00:00Added an answer on June 17, 2026 at 11:30 pm

    If you’re shrinking the size of your array, you would first need to free each myStructArray[i] where i >= num_elements, otherwise you will have a memory leak.

    Put another way, shrinking the size of the pointer array does not affect the memory that each array element was pointing to.

    Also, in the realloc call, you will want to assign the result to a temporary pointer; if realloc cannot extend or shrink the buffer, it will return NULL, and you’ll lose your reference to that block, which will also introduce a leak:

    myStruct **tmp = realloc(myStructArray, ...);
    if (tmp)
    {
      myStructArray = tmp;
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'
I know that nhibernate has a scheduler that will execute a function at certain
Basically, I have a block of memory allocated using malloc that I want to
I made a small function that fills an allocated block of memory containing every
Possible Duplicate: How do you realloc in C++? I know that C++ arrays can
I know that WCF is supposed to be transport agnostic, but is there a
I know that alloc and retain will increase the reference count of an object.
I need to know, are there any risks using the realloc() function to get
Possible Duplicate: How do malloc() and free() work? I read somewhere that calling free
I know that Phonegap has an event for back button, but it's only available

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.