Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 330 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regarding the differences between malloc and new in terms of their respective mechanisms of handling memory allocation?

#1
What are the differences between malloc and new in terms of their respective mechanisms of handling memory allocation?
Reply

#2
Well, `malloc()` is a more low-level primitive. It just gives you a pointer to *n* bytes of heap memory. The C++ `new` operator is more "intelligent" in that it "knows" about the type of the object(s) being allocated, and can do stuff like call constructors to make sure the newly allocated objects are all properly initialized.

Implementations of `new` often end up calling `malloc()` to get the raw memory, then do things on top of that memory to initalize the objecs(s) being constructed.
Reply

#3
If the call fails, `new` will throw an exception whereas `malloc` will return `NULL`.

For `malloc`, the caller has to specify the amount of memory to be allocated, while `new` automatically determines it.

These differences are concerning allocation, there are tons of others - new will call constructor, new can be overloaded, new is an operator whereas malloc is a function...
Reply

#4
Do you mean how they are implemented?

They can be implemented as anything, just `malloc` may not call `new`, and all standard `new`s must call the global `operator new(void*)`. Often `new` is even implemented as calling `malloc` but there is no requirement on how that is implemented. There are even dozens of allocators out there, each with some strengths and some weeknesses.

Or do you mean how they differ on the language level?

* `new` throws (unless it is called with `std::nothrow`) on allocation error. new expressions (not the operator new) calls the ctor.
* `malloc` returns `0` on allocation failures.
Reply

#5
- `malloc` doesn't throw `bad_alloc` exception as `new` does.
- Because malloc doesn't throw exceptions, you have to check its result against `NULL` (or nullptr in c++11 and above), which isn't necessary with `new`. However, `new` can be used in a way it won't throw expections, as when function `set_new_handler` is set
- `malloc` and `free` do not call object constructors and destructors, since there is no objects in `C`.
- see <a href="https://stackoverflow.com/questions/7443782/does-dynamic-memory-allocation-differ-in-c-and-c-in-popular-implementations/7444032#7444032">this question</a> and <a href="https://isocpp.org/wiki/faq/freestore-mgmt#new-malloc-diff">this post</a>.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through