   how new works (C++)
  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   
  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.   ;  so new work like this?void *operator new(size_t size){   void *ptr = (void *)malloc(size);};   ;  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   ;Quote:Original post by JasonL220so 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 vtables2) calls constructorsYou cannot easily re-implement the functionality of new.  just read my above post for what you want to do.-me   ;  Dunno if you've seen this article, but I came across this today, it gives a few details on new that probably aren't common knowledge: Link;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. ;Quote:Original post by perfectly_darkQuote: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().   ;  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.   ;Quote:Original post by PalidineQuote:Original post by JasonL220so 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 vtables2) calls constructorsYou cannot easily re-implement the functionality of new.  just read my above post for what you want to do.-meNote that there is a difference between "new" as a keyword, and "operator new".   