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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:55:54+00:00 2026-05-21T06:55:54+00:00

Is the cost of memory [de]allocation specifically defined? If the cost depends upon the

  • 0

Is the cost of memory [de]allocation specifically defined? If the cost depends upon the specific compiler being used, is there a general way memory [de]allocation is implemented such that I can reasonably assume the cost?

Is the compiler able to optimize the following code such that the call to ‘new’ is only done once?

char * arr = NULL;
for (size_t i = 0; i < 5000000000; ++i)
{
    arr = new char[100000000]
    ... // Process things here
    delete []arr;
}
  • 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-21T06:55:54+00:00Added an answer on May 21, 2026 at 6:55 am

    The compiler is almost certainly not able to perform this optimization. At the lowest level, storage allocation boils down to calls to library functions such as malloc (and, one layer deeper, to OS APIs). For the compiler it’s not safe to assume that individual malloc/free pairs can be left out and their storage reused because their implementation should be outside the optimizer’s scope.

    Apart from that, I don’t think this is a good job for the optimizer. That’s something that you, the programmer, can do without special efforts.

    There is no standardized cost for memory allocation/deallocation. Generally, allocation/deallocation times may vary greatly (for example it will take significantly longer if the user-space heap implementation is forced to fetch fresh pages from the OS kernel’s memory manager).

    A reasonable rule of thumb is that small allocations are most likely faster than larger ones and allocations should be slower than de-allocations.

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

Sidebar

Related Questions

Is there a way to find out how much memory is used by an
The memory cost obviously depends on exactly how large a module is, but I'm
I have a cost optimization request that I don't know how if there is
Is there any cost/drawback (apart from typing too much) to adorning a class with
Possible Duplicate: C++ virtual function table memory cost I've juste read that : http://www.parashift.com/c++-faq-lite/virtual-functions.html
What is additional memory cost of Tuple[Int, Int] ie. (1, 2) over two Ints
In C# is there any cost of storing ints (or any value types) as
Is cost accounting native to GP?
The following method calculates cost. It access other classes when required. Cloud class holds
What is the cost of len() function for Python built-ins? (list/tuple/string/dictionary)

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.