14926

Does C++ new operator use malloc() underneath?

<h3>Question</h3>

In other words, does it or not do a malloc() syscall everytime it is called ? (maybe by allocation a large chunk in advance)


<h3>Answer1:</h3>

Before C++14 the standard prohibited the implementation from combining allocations. Therefor each new expression did correspond one-to-one with a call to some system allocation function (possibly malloc).

C++14 relaxed this restriction in some cases. It's now possible for the implementation to combine allocations if the lifetime of one is strictly within the lifetime of the other. This is a fairly narrow restriction though, so I expect allocations don't actually get combined all that often.


<h3>Answer2:</h3>
<blockquote>

In other words, does it or not do a malloc() syscall everytime it is called?

</blockquote>

It's actually implementation dependend. But usually implementations of new will make use of malloc() syscalls/c-library bindings.

<blockquote>

(maybe by allocation a large chunk in advance)

</blockquote>

Yes, you have to consider that as a drawback. Frequently calling something like

char* newChar = new char();

may clutter your dynamic storage space unnecessarily with larger chunks allocated, than a single char would need.

If you want to override that behavior for some more efficient memory management, you can always use placement new.


<h3>Answer3:</h3>

As others have said, this is implementation defined. However, I would think that a high-performance C++ implementation would probably not use malloc(), but would use OS-specific memory allocation APIs or system calls (which malloc() must itself use). After all, why add an extra function call to every memory allocation? But I have no hard evidence for this.

来源:https://stackoverflow.com/questions/43957649/does-c-new-operator-use-malloc-underneath

Recommend

  • Slow initial connection to MS access database
  • Regex (JavaScript): match feet and/or inches
  • Searching a list of words from a large file in python
  • What is meaning of “:” in struct C [duplicate]
  • Does a developer have to pay for publishing a SaaS app on Windows Azure Marketplace?
  • Python argparse AssertionError when number of arguments exceeds threshold
  • Simple HTML DOM - Child Selectors (CSS)
  • Is there a particular naming convention for Java methods that throw exceptions?
  • ClickOnce is displaying the '.application' file as XML
  • Get list of person who shared a post on facebook
  • Bluetooth Low Energy device scanning Failed with an exception
  • Core Data Optimize Fetch Request
  • Angular: call function from other component
  • AngularJS Radio group not setting $dirty on field
  • Is it possible to use pseudo-elements to make containing elements wrap around an absolutely-position
  • twitter bootstrap for high resolution mobile displays
  • One x-axis tick every 7 points
  • Create intance of any C# class by generic way
  • mysql setting a negative integer value to a primary auto increment field which is int
  • RSRuntimeException: Loading of ScriptC script failed (build tools 19.0.2)
  • flexbox sticky footer
  • unicode characters in html
  • Updating DbSet item's value from the controller - C#, MVC, Code First
  • Should the client have the same keystore as the server?
  • Special characters (ë) in JSON-response
  • Validating a Firebase Key [duplicate]
  • javax.net.ssl.SSLException: SSL handshake aborted Connection reset by peer while calling webservice
  • What are advantages/disadvantages of using Selenium for Java vs .NET applications?
  • Stop an element moving with padding on hover
  • Possible to set default CloudKit container not based on application name?
  • How to make 100% div height between header and footer?
  • Neo4j…how to get a visual representation of my data?
  • Python 3x- Compression Makes File Bigger :(
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • WPF custom control and direct content support
  • Create/delete users from text file using Bash script
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?