neWs& delete’s u It's recommended using new delete For user-defined data types, it will not only allocate and de- allocate memory, but also invoke their ctors and dtors Never mix-use the new delete and malloc free, for they allocate spaces in different memory areas new and array new delete and array delete string stringPtr1= new string /one ctor called string* stringPtr2= new string[ 100] //100 ctors called delete stringPtr1 // delete a single object delete[ stringPtra //delete an object array, 100 dtors called
‘new’s & ‘delete’s ◼ It’s recommended using new & delete ◼ For user-defined data types, it will not only allocate and deallocate memory, but also invoke their ctors and dtors ◼ Never mix-use the new & delete and malloc & free, for they allocate spaces in different memory areas ◼ new and array new, delete and array delete string* stringPtr1 = new string; //one ctor called string* stringPtr2 = new string[100]; //100 ctors called … delete stringPtr1; //delete a single object delete [] stringPtr2; //delete an object array, 100 dtors called
news& delete's(cont u Use the same form in corresponding uses of new delete Mis-match will lead undefined behavior a Never to make typedef to arrays typedef string AddressLines[4] string* pal new AddressLines delete pal; //Undefined result, at least memory leak delete[ pal; / /OK, but seems strange
‘new’s & ‘delete’s (cont.) ◼ Use the same form in corresponding uses of new & delete ◼ Mis-match will lead undefined behavior ◼ Never to make typedef to arrays typedef string AddressLines[4]; string* pal = new AddressLines; … delete pal; //Undefined result, at least memory leak delete [] pal; //OK, but seems strange
Inside story of new? u new operator VS operator new(operator newl] u new operator, also called new expression, is a language defined behavior that can not be re-defined or overloaded string ps new string( Hello); 2 steps of the new operator behavior Allocate enough memory for the object(using operator new) Invoke its constructor to initialize operator new allocate certain size of raw memory and return the pointer void* operator new(size-t size) Programmer can define his own allocation behavior, that is, operator new could be overloaded The first parameter must be a size t
Inside story of ‘new’ ◼ new operator VS operator new(operator new[]) ◼ new operator, also called new expression, is a languagedefined behavior that can not be re-defined or overloaded string*ps = new string(“Hello”); ◼ 2 steps of the new operator behavior – Allocate enough memory for the object (using operator new) – Invoke its constructor to initialize ◼ operator new allocate certain size of raw memory, and return the pointer void* operator new(size_t size); ◼ Programmer can define his own allocation behavior, that is, operator new could be overloaded ◼ The first parameter must be a size_t
Inside story of new(cont,) u Pseudo code from the compiler for a new operator string ps= new string( Hello); void* memory operator new(sizeof(string) ca硎∥ string: string(“Heb”)0 memory string *ps static__cast<string *>(memory;
Inside story of ‘new’ (cont.) ◼ Pseudo code from the compiler for a new operator string*ps = new string(“Hello”); void* memory = operator new(sizeof(string)); call string::string(“Hello”) on *memory; string *ps = static_cast<string*>(memory);
Inside story of new(cont,) u Some advanced topics about new u Placement new-to construct objects at specified position char s[1000 void buffer= static_cast<void*>s new(buffer) string( Hi) a Exceptions thrown when failed in allocating enough memory new-handler-error handling function that invoked before throwing exceptions a Similar story in delete operator and operator delete
Inside story of ‘new’ (cont.) ◼ Some advanced topics about ‘new’ ◼ Placement new – to construct objects at specified position char s[1000]; void* buffer = static_cast<void*>(s); new (buffer) string(“Hi”); ◼ Exceptions thrown when failed in allocating enough memory ◼ new-handler – error handling function that invoked before throwing exceptions ◼ Similar story in delete operator and operator delete