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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:00:56+00:00 2026-05-26T22:00:56+00:00

In common implementations such as Linux/Glibc, Windows/MSVC and BSD/Mac OS X, will void *p

  • 0

In common implementations such as Linux/Glibc, Windows/MSVC and BSD/Mac OS X, will

void *p = malloc(N + M);  // assume this doesn't fail
p = realloc(p, N);        // nor this

for N, M > 0, actually shrink the buffer returned by malloc in the realloc call, in the sense that up to M bytes may return to the free list? And more importantly, is there a chance that it reallocates the buffer?

I want to know because I just implemented dynamic arrays on top of numpy.ndarray, and I’m doing a resize, which calls realloc, to get the final size right. I may be able to skip the final resize as an optimization (at the expense of permanent overallocation) and I want to know if that’s even worth trying.

  • 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-26T22:00:57+00:00Added an answer on May 26, 2026 at 10:00 pm

    I can say about Linux/glibc.
    In the source code it contains comments like this:

    if n is for fewer bytes than already held by p, the newly unused
    space is lopped off and freed if possible.

    if you look at code of glibc, it contains lines like this:

    remainder_size = newsize - nb;
    
    if (remainder_size < MINSIZE) { /* not enough extra to split off */
      set_head_size(newp, newsize | (av != &main_arena ? NON_MAIN_ARENA : 0));
      set_inuse_bit_at_offset(newp, newsize);
    }
    else { /* split remainder */
      remainder = chunk_at_offset(newp, nb);
      set_head_size(newp, nb | (av != &main_arena ? NON_MAIN_ARENA : 0));
      set_head(remainder, remainder_size | PREV_INUSE |
           (av != &main_arena ? NON_MAIN_ARENA : 0));
      /* Mark remainder as inuse so free() won't complain */
      set_inuse_bit_at_offset(remainder, remainder_size);
     #ifdef ATOMIC_FASTBINS
      _int_free(av, remainder, 1);
     #else
      _int_free(av, remainder);
     #endif
    }
    

    nb – number of bytes you want, newsize here, should be called oldsize.
    So it tries to free the excess if possible.

    About Mac OSX. More precisely about magazine_malloc, current implementation of malloc from Apple. See http://cocoawithlove.com/2010/05/look-at-how-malloc-works-on-mac.html for details.

    realloc calls the zone realloc method, its current implementation as I see is szone_realloc.
    For different allocation sizes exists different code, but the algorithm is always the same:

    if (new_good_size <= (old_size >> 1)) {
                /*
                 * Serious shrinkage (more than half). free() the excess.
                 */
                return tiny_try_shrink_in_place(szone, ptr, old_size, new_good_size);
    } else if (new_good_size <= old_size) {
                /* 
                 * new_good_size smaller than old_size but not by much (less than half).
                 * Avoid thrashing at the expense of some wasted storage.
                 */
                 return ptr;
    }
    

    So as you can see, its implementation checks that new_size <= old_size / 2, and if so frees memory, and if not it does nothing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any Common Lisp implementations for .Net?
I think this question is quite common. Recently I have got a requirement that
Anyone know the time-complexity of ECMAScript5's Object.keys() in common implementations? Is it O(n) for
I've been searching for information for a common kernel implementation of queues, that is,
After implementing most of the common and needed functions for my Graph implementation, I
Common question but I could use an english explanation. Is it like Java where
Common scenario: I have a library that uses other libraries. For example, a math
Common problem I'm sure, but I can't figure it out. In my AndroidManifest.xml and
Common situation: I have a client on my server who may update some of
A common task in programs I've been working on lately is modifying a text

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.