Doesn't new operator allocate heap memory

L

lovecreatesbea...

If the built-in operator keyword new doesn't allocate memory on heap
and it calls global operator new :):eek:perator new) or class member
operator new to do that. What are the two kinds of operator new used to
allocate heap memory?

Before there aren't global and member operator new, the built-in
operator keyword new does really allocate heap memory, right?
 
G

Gianni Mariani

If the built-in operator keyword new doesn't allocate memory on heap
and it calls global operator new :):eek:perator new) or class member
operator new to do that. What are the two kinds of operator new used to
allocate heap memory?

Before there aren't global and member operator new, the built-in
operator keyword new does really allocate heap memory, right?

It might be called somthing else but for most intents and purposes it
behaves just like memory allocated by malloc/free. You can't use free()
to release memory allocated by operator new or visa versa tho.
 
B

benben

If the built-in operator keyword new doesn't allocate memory on heap
and it calls global operator new :):eek:perator new) or class member
operator new to do that. What are the two kinds of operator new used to
allocate heap memory?

This is the confusing part of C++. When you have something like:

T* ptr = new T;

the followings are performed:

1. Either the default operator new or an overloaded version is called
to obtain raw memory large enough to fit a T object.
2. The constructor of T is called.

The operator new, as in the function you can overload, only allocates
raw memory. The keyword new does that plus constructing the object.

By default, new allocates memory from the heap, or the free store. You
can overloaded either globally or specific to a class to provide
alternate memory management, usually to

- tweak performance
- inspect memory allocation
- debug the program
Before there aren't global and member operator new, the built-in
operator keyword new does really allocate heap memory, right?

That, I don't know.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top