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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:05:24+00:00 2026-05-23T05:05:24+00:00

We know that the stack of a program will grow or shrink when it

  • 0

We know that the stack of a program will grow or shrink when it runs. In a C program, when we use malloc() to allocate memory, if current memory is not enough, it will call sbrk() to extend the heap size. When we use free() to release allocated memory, it doesn’t shrink the heap. Why doesn’t it make sense to shrink the heap?

  • 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-23T05:05:24+00:00Added an answer on May 23, 2026 at 5:05 am

    The stack does not shrink. Your use of the stack may be variable but the stack itself generally stays constant in size.

    And you can shrink the heap by calling sbrk with a negative argument but I suspect the main reason that’s not done is because the process may need the memory again at some point. It can take time to adjust the malloc arena(s) when the underlying memory changes.

    When you need more memory, that’s fine, you pay the price because you want something. But you don’t want to pay that price when releasing memory since you don’t need to. And, if you did, then needed that memory again, you’d be constantly paying the price. Think of the loop:

    for (int i = 0; i < 1000; i++) {
        char *m = malloc (1000000);
        free (m);
    }
    

    and think of how much more inefficient that would be with the extra load.

    You can think of the memory freed but not released back to the operating system as your own personal cache of memory.

    This is all assuming, of course, that malloc uses sbrk at all. Modern operating systems may provide better alternatives due to the disconnect between logical and physical memory.

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

Sidebar

Related Questions

Does anyone know if the problems that have been affecting Stack Overflow with regards
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I am writing a program that will query an MS access database, return the
I know that every running process has pages associated with it in virtual memory
I am looking for a program or API call that will give me, with
I know that I can do something like $int = (int)99; //(int) has a
I know that default cron's behavior is to send normal and error output to
I know that you can insert multiple rows at once, is there a way
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that .NET is JIT compiled to the architecture you are running on

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.