Jump to content
  • Advertisement
Sign in to follow this  
JasonL220

how new works (C++)

This topic is 4546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm looking at making a simple memory checker to improve my knowledge of the language, but i can't find any good doc's (the msdn tells me how to use it but not how it works) on how the new operator and variants function so i don't know how to implement my own debug version. does anyone know of any good docs, or have any other suggestions? thanks

Share this post


Link to post
Share on other sites
Advertisement
The default new usually calls malloc() to allocate memory for the object, then it calls the object's constructor. There isn't any more to it than that.

Share this post


Link to post
Share on other sites
if you want a "debug" version of new, simply overload the new keyword to do your debug stuff. The default functionality will still happen. If you're doing a memory manager you'll want to look at placement new (basically you tell new where in memory to create your object: general usage is allocate a giant block of memory, keep it defragmented by moving stuff around and using placement new so that new objects are created in that pre-allocated block.

[google] "overload new C++" and "placement new"

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by JasonL220
so new work like this?

void *operator new(size_t size)
{
void *ptr = (void *)malloc(size);
};


No! It's a built in keyword to the language. It doesn't use malloc. it also:

1) conctructs vtables
2) calls constructors

You cannot easily re-implement the functionality of new. just read my above post for what you want to do.

-me

Share this post


Link to post
Share on other sites
Quote:

No! It's a built in keyword to the language. It doesn't use malloc.


Not true.
It is a built in keyword, but it still uses malloc (at least VC.NET 2003's implementation does). You can check this by stepping into a call to new in the debugger.

Share this post


Link to post
Share on other sites
Quote:
Original post by perfectly_dark
Quote:

No! It's a built in keyword to the language. It doesn't use malloc.

Not true.
It is a built in keyword, but it still uses malloc (at least VC.NET 2003's implementation does). You can check this by stepping into a call to new in the debugger.

The new keyword != operator new.

The new keyword obtains storage by calling an allocation function, defined in section 3.7.3 to be the various forms of operator new(). How these functions obtain their storage is unspecified except that it shall be suitably aligned, at least as large as the requested size, not a null pointer for a size of 0, and that should the allocation fail it shall call the currently installed new_handler.

The new keyword is responsible for the construction of the type allocated as well, not operator new().

Share this post


Link to post
Share on other sites
You have to make a difference between the keyword new and the operator new, they're different things.
The keyword new allocates memory with an operator new call and then calls the constructor of the object.

Most compilers come with special debug-new implementations. You can pass parameters to operator new in the new-call, e.g. foo* p = new (std::nothrow) foo;

Writing an own memory manager is fairly advanced and you shouldn't start with overloading the new operator. Start by writing custom allocators (what an allocator is can be seen in the STL, where they're used rather than plain new-calls). They're much clearer then playing with the operator overloading.

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
Quote:
Original post by JasonL220
so new work like this?

void *operator new(size_t size)
{
void *ptr = (void *)malloc(size);
};


No! It's a built in keyword to the language. It doesn't use malloc. it also:

1) conctructs vtables
2) calls constructors

You cannot easily re-implement the functionality of new. just read my above post for what you want to do.

-me


Note that there is a difference between "new" as a keyword, and "operator new".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!